2016-04-25 114 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

相关问题