使用hibernate通过注释生成一对多关系

时间:2012-04-10 16:09:26

标签: hibernate hibernate-mapping hibernate-annotations

我只是学习Spring MVC和Hibernate技术的初学者。 我正在尝试使用hibernate来访问我的数据库,我正在使用注释方法将我的bean配置为数据库。我试图做出一对多的关系,这让我犯了错误。

基本上我是一个"一个用户可以发布多个测验"关系

配置休眠时的错误是:
org.hibernate.AnnotationException:使用@OneToMany或@ManyToMany定位未映射的类:bean.User.quizPosted [bean.Quiz] 使用@OneToMany或@ManyToMany定位未映射的类:bean.User.quizPosted [bean.Quiz]

我的两个bean的代码是:

User.java

    package bean;

    import java.util.HashSet;
    import java.util.Set;

    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
    import javax.persistence.JoinColumn;
    import javax.persistence.ManyToOne;
    import javax.persistence.OneToMany;
    import javax.persistence.Table;
    import javax.persistence.UniqueConstraint;

    @Entity
    @Table(name="User", uniqueConstraints ={@UniqueConstraint(columnNames="userName"})})
    public class User {
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        private Integer id;

        @ManyToOne(targetEntity=Person.class)
        @JoinColumn(name="PersonId")
        private Person person;

        @Column(name="userName")
        private String userName;

        @Column(name="password")
        private String password;

        @Column(name="role")
        private String role;

        @OneToMany(mappedBy = "postedBy")
        private Set<Quiz> quizPosted = new HashSet<Quiz>();

        @OneToMany(mappedBy = "userId")
        private Set<User_Quiz> user_Quizs = new HashSet<User_Quiz>();
    }

和quiz.java

    package bean;

    import java.sql.Date;
    import java.util.HashSet;
    import java.util.Set;

    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
    import javax.persistence.JoinColumn;
    import javax.persistence.ManyToOne;
    import javax.persistence.OneToMany;
    import javax.persistence.Table;
    import javax.persistence.UniqueConstraint;


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

        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        private Integer quizId;

        @Column(name = "duration")
        private Integer duration;

        @Column(name = "date")
        private Date date;

        @ManyToOne(targetEntity = User.class)
        @JoinColumn(name = "postedBy")
        private User postedBy;

        @OneToMany(mappedBy="quizId")
        private Set<User_Quiz> user_Quizs = new HashSet<User_Quiz>();        

    }

1 个答案:

答案 0 :(得分:0)

您在Defined annotated class中有未编写的类,这意味着您在OnetoMany和ManyToOne关系中使用的所有类必须在Application Context中定义,因为您已经定义了使用spring。在您的会话工厂中,在annotatedClasses属性中添加ur类。

 <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
   <property name="dataSource" ref="dataSource"/>
   <property name="annotatedClasses">
     <list>
       <value>test.package.Foo</value>
       <value>test.package.Bar</value>
     </list>
   </property>
 </bean>

请参阅Annotated Session Factory