2015-04-02 35 views
-1

我有以下2個實體:空的集合


@Entity 
public class User implements java.io.Serializable { 

    private Integer iduser; 
    private String email; 
    private String password; 
    private Byte enabled; 
    private Set<Token> tokens = new HashSet<>(0); 

    public User() { 
    } 

    public User(String email, String password, Byte enabled/*, Set groupRights*/, Set tokens) { 
     this.email = email; 
     this.password = password; 
     this.enabled = enabled; 
     this.tokens = tokens; 
    } 

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

    public void setIduser(Integer iduser) { 
     this.iduser = iduser; 
    } 

    @Column(name = "email", unique = true, length = 45) 
    public String getEmail() { 
     return this.email; 
    } 

    public void setEmail(String email) { 
     this.email = email; 
    } 

    @Column(name = "password", length = 60) 
    public String getPassword() { 
     return this.password; 
    } 

    public void setPassword(String password) { 
     this.password = password; 
    } 

    @Column(name = "enabled") 
    public Byte getEnabled() { 
     return this.enabled; 
    } 

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

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "user") 
    public Set<Token> getTokens() { 
     return this.tokens; 
    } 

    public void setTokens(Set<Token> tokens) { 
     this.tokens = tokens; 
    } 

} 

@Entity 
public class Token implements java.io.Serializable { 

    private String idtoken; 
    private User user; 
    private Date tokenTtl; 
    private String ipLock; 

    public Token() { 
    } 

    public Token(String idtoken) { 
     this.idtoken = idtoken; 
    } 

    public Token(String idtoken, User user, Date tokenTtl, String ipLock) { 
     this.idtoken = idtoken; 
     this.user = user; 
     this.tokenTtl = tokenTtl; 
     this.ipLock = ipLock; 
    } 

    @Id 
    @Column(name = "idtoken", unique = true, nullable = false, length = 36) 
    public String getIdtoken() { 
     return this.idtoken; 
    } 

    public void setIdtoken(String idtoken) { 
     this.idtoken = idtoken; 
    } 

    @ManyToOne(fetch = FetchType.LAZY) 
    @JoinColumn(name = "user_id") 
    public User getUser() { 
     return this.user; 
    } 

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

    @Temporal(TemporalType.TIMESTAMP) 
    @Column(name = "token_ttl", length = 19) 
    public Date getTokenTtl() { 
     return this.tokenTtl; 
    } 

    public void setTokenTtl(Date tokenTtl) { 
     this.tokenTtl = tokenTtl; 
    } 

    @Column(name = "ip_lock", length = 45) 
    public String getIpLock() { 
     return this.ipLock; 
    } 

    public void setIpLock(String ipLock) { 
     this.ipLock = ipLock; 
    } 

} 

的問題是,當我選擇使用此JPA-QL用戶:select u from User u,我得到一個空SetToken相關,即使此用戶有相關的令牌。 只有在Spring 4上下文中獲得JPA上下文(EntityManager)時纔會出現此問題。如果我正在進行測試,直接創建EntityManager(使用此:Persistence.createEntityManagerFactory("unit-name");),則此問題不存在。

有人可以告訴我什麼是我的問題的原因?

+0

你有沒有試着用級聯= CascadeType.ALL在@ManyToOne令牌實體(取= FetchType.LAZY) – 2015-04-02 11:28:56

回答

1

你需要這樣

private Set<Token> tokens = new HashSet<Token>(0); 

創建消氣二傳手使用accordingley

@OneToMany(fetch = FetchType.LAZY, mappedBy = "user") 
    public Set<Token> getTokens() { 
     return this.tokens; 
    } 

    public void setTokens(Set<Token>tokens) { 
     this.tokens = tokens; 
    } 
+0

這只是一個標籤顯示的問題。我已經在我的代碼中。 – artaxerxe 2015-04-02 11:37:26

0

問題與從OpenJPA的一個bug。 當您在OpenJPA中創建與FetchType.EAGER獲取類型的雙向@OneToMany關係時,由於無法創建此關係而出現該錯誤。該錯誤在某些版本中得到修復。似乎會影響版本2.1.0,2.2.2,2.3.0和2.4.0。

參考文獻:

https://issues.apache.org/jira/browse/OPENJPA-2505

Spring Data JPA OneToMany and ManyToOne give me JpaSystemException

+0

雖然這個鏈接可能回答這個問題,但最好在這裏包含答案的基本部分,並提供參考鏈接。如果鏈接頁面更改,則僅鏈接答案可能會失效。 – Magnilex 2015-04-02 16:12:37

+0

@Magnilex謝謝。希望現在好一點。 – artaxerxe 2015-04-02 16:23:24