2016-04-25 55 views
0


我最近一直在研究Hibernate和MVC。我按照各種教程,並嘗試做了許多一對多的關係,但我不斷收到以下錯誤信息:Spring + Hibernate無法確定類型設置

Caused by: org.hibernate.MappingException: Could not determine type for: java.util.Set, at table: Group, for column 

在我的servlet-context.xml中,我有以下幾點:

<beans:list> 
       <beans:value>com.model.Account</beans:value> 
       <beans:value>com.model.Group</beans:value> 
      </beans:list> 

在我Group.class我有以下幾點:

@Entity 
@Table(name="Group") 
public class Group { 
    @Id 
    @Column(name="group_id") 
    @GeneratedValue(strategy=GenerationType.IDENTITY) 
    private int id; 

    private String objectId; 
    private String groupName; 
    private Set<Account> accountList = new HashSet<Account>(); 
    // ... Getters and setters 

    @ManyToMany(cascade=CascadeType.ALL) 
    @JoinTable(name="objectId", [email protected](name="group_id"), 
     [email protected](name="objectId")) 
    public Set<Account> getAccountList() { 
     return accountList; 
    } 
} 

修訂
Account.clas小號

@Entity 
@Table(name="Account") 
public class Account { 

    @Column(name="id") 
    @GeneratedValue(strategy=GenerationType.IDENTITY) 
    private int id; 

    @Id 
    private String objectId; 

    @ManyToMany(cascade=CascadeType.ALL, mappedBy="accountList") 
    private Set<Group> groupList = new HashSet<Group>(); 
//...Getters and Setters 

Group.class

@Entity 
    @Table(name="Group") 
    public class Group { 
     @Id 
     @Column(name="id") 
     @GeneratedValue(strategy=GenerationType.IDENTITY) 
     private int id; 

     private String objectId; 
     private String groupName; 
     @ManyToMany(cascade=CascadeType.ALL) 
     @JoinTable(name="objectId", [email protected](name="group_id"), 
      [email protected](name="objectId")) 
     private Set<Account> accountList = new HashSet<Account>(); 

    } 

現在我得到以下錯誤每次我嘗試從DAO實現節省時間:

Request processing failed; nested exception is org.springframework.dao.DataIntegrityViolationException: Duplicate entry '1' for key 'PRIMARY'; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: Duplicate entry '1' for key 'PRIMARY' 

我相信,設置導致這個問題,但各種教程能夠完成它,是否有一些額外的程序,我需要做的。

回答

2

由於您在字段中給出了@Id註釋,因此您必須在字段級別而不是在getter處定義其他註釋。

@ManyToMany(cascade=CascadeType.ALL) 
@JoinTable(name="objectId", [email protected](name="group_id"), 
    [email protected](name="objectId")) 
private Set<Account> accountList = new HashSet<Account>(); 

將解決您的問題。

不要忘記在Account類中也要遵循相同的步驟。

+0

嗨,我收到一個新的外鍵映射例外。我是否在Account.class的字段級別上註釋了我的註釋?查看更新部分 – user288231

+0

你能分享你的DAO實現的相關代碼嗎? –

+0

我已修復他的問題。我不得不做一個@JsonIgnore,它崩潰了,因爲它進入了一個無限循環。謝謝! – user288231

1

可以爲@ManyToMany提供您的帳戶類定義嗎?

+0

我已將我的帳戶類@ManyToMany關係。我仍然不知道爲什麼我會得到映射錯誤。 – user288231

相關問題