2016-01-29 135 views
2

一个关系我有数据库架构的这一部分: schemaUser实体:一个与Hibernate的注解

@Entity 
@Table(name = "user", catalog = "ats") 
public class User implements java.io.Serializable{ 

    private static final long serialVersionUID = 1L; 
    private String username; 
    private boolean enabled; 
    private Role role; 
    private ClientVersion clientVersion; 
    private ClientLicense clientLicense; 
    @JsonIgnore 
    private Set<NotificationHasUser> notificationHasUsers = new HashSet<NotificationHasUser>(0); 

    public User() { 
    } 

    public User(String username, boolean enabled) { 
     this.username = username; 
     this.enabled = enabled; 
    } 

    public User(String username, boolean enabled, Role role, Set<NotificationHasUser> notificationHasUsers) { 
     this.username = username; 
     this.enabled = enabled; 
     this.role = role; 
     this.notificationHasUsers = notificationHasUsers; 
    } 

    @Id 
    @Column(name = "username", unique = true, nullable = false, length = 45) 
    public String getUsername() { 
     return this.username; 
    } 

    public void setUsername(String username) { 
     this.username = username; 
    } 

    @Column(name = "enabled", nullable = false) 
    public boolean isEnabled() { 
     return this.enabled; 
    } 

    public void setEnabled(boolean enabled) { 
     this.enabled = enabled; 
    } 

    @ManyToOne(fetch = FetchType.LAZY) 
    @JoinColumn(name = "id_role", nullable = false) 
    public Role getRole() { 
     return this.role; 
    } 

    public void setRole(Role role) { 
     this.role = role; 
    } 

    @ManyToOne(fetch = FetchType.LAZY) 
    @JoinColumn(name = "id_clientVersion", nullable = false) 
    public ClientVersion getClientVersion() { 
     return this.clientVersion; 
    } 

    public void setClientVersion(ClientVersion clientVersion) { 
     this.clientVersion = clientVersion; 
    } 

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "pk.user") 
    public Set<NotificationHasUser> getNotificationHasUser() { 
     return this.notificationHasUsers; 
    } 

    public void setNotificationHasUser(Set<NotificationHasUser> notificationHasUsers) { 
     this.notificationHasUsers = notificationHasUsers; 
    } 

    @OneToOne(fetch = FetchType.LAZY, mappedBy = "user") 
    public ClientLicense getClientLicense(){ 
     return this.clientLicense; 
    } 

    public void setClientLicense(ClientLicense clientLicense){ 
     this.clientLicense = clientLicense; 
    } 
} 

一切工作正常,直到我添加了一个新的clientlicense。如果我添加此我收到一个无限循环:

Could not write content: Infinite recursion (StackOverflowError) (through reference chain: com.domain.User["clientLicense"]->com.domain.ClientLicense["user"]->com.domain.User["clientLicense"]->com.domain.ClientLicense["user"]->com.domain.User["clientLicense"]->com.domain.ClientLicense["user"]->com.domain.User["clientLicense"]->com.domain.ClientLicense["user"]->com.domain.User["clientLicense"]-.... 

这是我ClientLicense实体

@Entity 
@Table(name = "clientlicense", catalog = "ats") 
public class ClientLicense implements java.io.Serializable{ 

    /** 
    * 
    */ 
    private static final long serialVersionUID = 1L; 
    private Integer idClientLicense; 
    private Date startDate; 
    private Date endDate; 
    private int counter; 
    private String macAddress; 
    private String cpuId; 
    private User user; 

     public ClientLicense() { 
     } 

     /** 
     * @param startDate 
     * @param endDate 
     * @param counter 
     * @param macAddress 
     * @param cpuId 
     * @param users 
     */ 
     public ClientLicense(Date startDate, Date endDate, int counter, String macAddress, String cpuId, User user) { 
      super(); 
      this.startDate = startDate; 
      this.endDate = endDate; 
      this.counter = counter; 
      this.setMacAddress(macAddress); 
      this.setCpuId(cpuId); 
      this.user = user; 
     } 

     @Id 
     @GeneratedValue(strategy = IDENTITY) 
     @Column(name = "id_clientLicense", unique = true, nullable = false) 
     public Integer getIdClientLicense() { 
      return this.idClientLicense; 
     } 

     public void setIdClientLicense(Integer idClientLicense) { 
      this.idClientLicense = idClientLicense; 
     } 


     @Column(name = "startDate", nullable = false) 
     public Date getStartDate() { 
      return this.startDate; 
     } 

     public void setStartDate(Date startDate) { 
      this.startDate = startDate; 
     } 

     @Column(name = "endDate", nullable = false) 
     public Date getEndDate() { 
      return this.endDate; 
     } 

     public void setEndDate(Date endDate) { 
      this.endDate = endDate; 
     } 


     @Column(name = "counter", nullable = false) 
     public int getCounter() { 
      return this.counter; 
     } 

     public void setCounter(int counter) { 
      this.counter = counter; 
     } 

     /** 
     * @return the macAddress 
     */ 
     @Column(name = "macAddress", nullable = false) 
     public String getMacAddress() { 
      return macAddress; 
     } 

     /** 
     * @param macAddress the macAddress to set 
     */ 
     public void setMacAddress(String macAddress) { 
      this.macAddress = macAddress; 
     } 

     /** 
     * @return the cpuId 
     */ 
     @Column(name = "cpuId", nullable = false) 
     public String getCpuId() { 
      return cpuId; 
     } 

     /** 
     * @param cpuId the cpuId to set 
     */ 
     public void setCpuId(String cpuId) { 
      this.cpuId = cpuId; 
     } 

     @OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL) 
     @JoinColumn(name = "id_username") 
     public User getUser() { 
      return this.user; 
     } 

     public void setUser(User user) { 
      this.user = user; 
     } 
    } 

这是我第OneToOne关系,什么是正确的注解,我必须使用?我读了一些例子,但我不明白,他们是不同的。

+0

不相同的列“的映射”'''ClientLicense'''需要的' ''用户''对象?或者只能使用用户ID? – DominicEU

+1

尝试把'@ JsonIgnore'上'ClientLicense#user' –

+0

我把这个注释的用户类和它的作品,但我不能添加新clientLicense,我更新了我的问题用一个例子 – luca

回答

0

尝试这样的事情。

public class User { 
    private ClientLicense clientLicense; 

    @OneToOne(fetch = FetchType.LAZY, mappedBy = "user") 
    public ClientLicense getClientLicense() { 
     return this.clientLicense; 
    } 
} 

public class ClientLicense { 
    private User user; 

    @OneToOne 
    @JoinColumn(name = "id_username") 
    public User getUser() { 
     return this.user; 
    } 
} 
+0

它不工作,他无限循环的问题是json注释。 – luca

0

问题是,两个实体没有办法发现这两个字段实际上是指定单个关系。因此,hibernate假定它们不是相同的关系,因此试图获取它们(因为默认情况下一对一关系会被急切地取回)。

clientLicense前场添加@OneToOne(mappedBy = "user")User类告诉Hibernate,这个字段作为user字段中ClientLicense

+0

mappedBy =“user”已经设置 – luca

+0

@luca没有'@ JoinColumn'注解? – emjay

+0

@ JoinColumn在ClientLicense实体 – luca