无法创建hibernate中的复合主键

时间:2011-11-06 13:10:01

标签: hibernate composite-key

我正在尝试创建这些情况: 用户-------->用户对Author< ------------ Author

的评价

所以我上课了:

public class User implements UserDetails {

        @Id
    private String username;
        (...)
        @OneToMany(targetEntity=RateAth.class, mappedBy="user", cascade=CascadeType.ALL)
        List<RateAth> authorRates

public class Author {

        @Id
    private int id;
    (...)
        @OneToMany(targetEntity=RateAth.class, mappedBy="author", cascade=CascadeType.ALL)
    private List<RateAth> authorRates;

这是我的RateAth课程:

@Entity
@Table(name="users_authors_rate")
public class RateAth {
    @Id
    private PK primaryKey;
    private Integer rate;
    private Date date;
    private String username;

    @Id
    public PK getPrimaryKey() {
        return primaryKey;
    }
    public void setPrimaryKey(PK primaryKey) {
        this.primaryKey = primaryKey;
    }
    public Integer getRate() {
        return rate;
    }
    public void setRate(Integer rate) {
        this.rate = rate;
    }
    public Date getDate() {
        return date;
    }
    public void setDate(Date date) {
        this.date = date;
    }
    public User getUser() {
        return primaryKey.getUser();
    }
    public void setUser(User user) {
        this.primaryKey.setUser(user);
    }

    public Author getAuthor() {
        return primaryKey.getAuthor();
    }
    public void setAuthor(Author author) {
        this.primaryKey.setAuthor(author);
    }

    }
@Embeddable 
class PK implements Serializable{
    @ManyToOne
    @JoinColumn(name="username")
    private User user;
    @ManyToOne
    @JoinColumn(name="author_id")
    private Author author;

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

    public Author getAuthor() {
        return author;
    }
    public void setAuthor(Author author) {
        this.author = author;
    }


}
  

... 49更多引起:org.hibernate.MappingException:不能   确定类型:beans.Author,在表:users_authors_rate,for   columns:[org.hibernate.mapping.Column(author)] at   org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:292)at   org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:276)at at   org.hibernate.mapping.Property.isValid(Property.java:207)at   org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:458)     在org.hibernate.mapping.RootClass.validate(RootClass.java:215)at   org.hibernate.cfg.Configuration.validate(Configuration.java:1135)at   org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1320)     在   org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:867)     在   org.springframework.orm.hibernate3.LocalSessionFactoryBean.newSessionFactory(LocalSessionFactoryBean.java:860)     在   org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:779)     在   org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:211)     在   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1477)     在   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1417)     ......还有56个

1 个答案:

答案 0 :(得分:0)

阅读http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html_single/#d0e4865

@Id注释必须放在字段或getter上,但不能同时放在两者上。 它必须由@EmbeddedId替换,因为它是嵌入对象而不是单个列字段。

我会使该类不可变并从实体中删除setAuthorsetUser方法,因为您无法更改实体的主键。