2014-05-07 65 views
0

有兩個實體:用戶和員工。用戶具有Employee類型的字段。序列化JPA實體 - 僅保存獲取實體的ID

@Entity 
@Table(name="user") 
public class User extends AuditableEntity {  
    Long idUser; 
    String username; 
    String password; 
    Employee employee; 

    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    public Long getIdUser() { return idUser; } 
    public void setIdUser(Long idUser) { this.idUser = idUser; } 

    @Column(name = "username") 
    public String getUsername() { return username; } 
    public void setUsername(String username) { this.username = username; } 

    @Column(name = "password") 
    public String getPassword() { return password; } 
    public void setPassword(String password) { this.password = password; } 

    @ManyToOne 
    @JoinColumn(name = "idemployee")  
    public Employee getEmployee() { return employee; } 
    public void setEmployee(Employee employee) { this.employee = employee; } 
} 

而且

@Entity 
@Table(name = "employee") 
public class Employee extends AuditableEntity { 
    Long  idEmployee; 
    String  surname; 
    String  name; 
    String  patronymic; 
    Date  birthdate; 

    @Id 
    @GeneratedValue (strategy=GenerationType.IDENTITY) 
    @Column(name = "idemployee") 
    public Long getIdEmployee() { return idEmployee; } 
    public void setIdEmployee(Long idEmployee) { this.idEmployee = idEmployee; } 

    @Column(name = "surname") 
    public String getSurname() { return surname; } 
    public void setSurname(String surname) { this.surname = surname; } 

    @Column(name = "name") 
    public String getName() { return name; } 
    public void setName(String name) { this.name = name; } 

    @Column(name = "patronymic") 
    public String getPatronymic() { return patronymic; } 
    public void setPatronymic(String patronymic) { this.patronymic = patronymic; } 

    @JsonFormat(pattern = "dd.MM.yyyy") 
    @Column(name = "birthday") 
    public Date getBirthdate() { return birthdate; } 
    public void setBirthdate(Date birthdate) { this.birthdate = birthdate; } 
} 

我需要序列化到用戶XML/JSON。我使用JAXB,但它的序列化員工太:

<User> 
    <idUser>15</idUser> 
    <username>user15</username> 
    <password>password15</password> 
    <employee> 
     <idEmployee>23</idEmployee> 
     <surname>Smith</surname> 
     <name>John</name> 
     <patronymic>H.</patronymic> 
     <birthdate>01.01.1970</birthdate> 
    </employee> 
<User> 

我需要的結果是這樣的:

<User> 
    <idUser>15</idUser> 
    <username>user15</username> 
    <password>password15</password> 
    <idEmployee>23</idEmployee>  
<User> 

我試圖用@XmlID,@XmlIDREF - 但它只能與字符型柱。 也試圖使用@XmlTransient - 但它只是排除Employee。 我如何序列化沒有員工的用戶,只有idEmployee?

第二個問題是反序列化。有沒有任何標準的方法來做到這一點?

回答

0

註釋屬性employee在類User@XmlTransient。此註釋可以應用於字段或其獲取者。

+0

這不包括整場做到這一點。但我需要保存員工的ID。 – Nikolay

0

可以爲僱員的ID添加額外的吸氣劑User實體

public class User 
{ 
    ... 
    @Transient // for JPA 
    @XmlElement 
    Long getIdEmployee() 
    { 
     return employee.getIdEmployee(); 
    } 
} 

或者你可以用@XmlValue

public class User 
{ 
    ... 
    @XmlElement(name = "idEmployee") 
    public Employee getEmployee() { return employee; } 
    public void setEmployee(Employee employee) { this.employee = employee; } 
} 

@XmlAccessorType(XmlAccessType.NONE) // to prevent marshalling of all properies 
public class Employee 
{ 
    ... 
    @XmlValue 
    public Long getIdEmployee() { return idEmployee; } 
    ... 
}