Hibernate / JPA中的一对多关系

时间:2012-07-03 09:35:53

标签: java hibernate jpa

我刚接触hibernate,我使用了两个有人和部分的类,但我能够将它们联系起来,我在线程“main”org.hibernate.AnnotationException中得到类似Exception:使用@OneToMany或@ManyToMany定位未映射的类:in.quadra.apps.Profile.Sections [in.quadra.apps.Section]

Person.java

package in.quadra.apps;

import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

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

    @Id
    @GeneratedValue
    @Column(name="Profile_ID")
    private Long ProfileId;

    @Column(name="Profile_NAME")
    private String ProfileName;

    @OneToMany(mappedBy="Profile")
    private Set<Section> Sections;

    public Long getProfileId() {
        return ProfileId;
    }

    public void setProfileId(Long profileId) {
        ProfileId = profileId;
    }

    public String getProfileName() {
        return ProfileName;
    }

    public void setProfileName(String profileName) {
        ProfileName = profileName;
    }

    public Set<Section> getSections() {
        return Sections;
    }

    public void setSections(Set<Section> sections) {
        Sections = sections;
    }

}

Section.Java

package in.quadra.apps;

import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

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

    @Id
    @GeneratedValue
    @Column(name="Profile_ID")
    private Long ProfileId;

    @Column(name="Profile_NAME")
    private String ProfileName;

    @OneToMany(mappedBy="Profile")
    private Set<Section> Sections;

    public Long getProfileId() {
        return ProfileId;
    }

    public void setProfileId(Long profileId) {
        ProfileId = profileId;
    }

    public String getProfileName() {
        return ProfileName;
    }

    public void setProfileName(String profileName) {
        ProfileName = profileName;
    }

    public Set<Section> getSections() {
        return Sections;
    }

    public void setSections(Set<Section> sections) {
        Sections = sections;
    }

}

main.java

package in.quadra.apps;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class Main123 {
    public static void main(String ar[]){
        SessionFactory sessionFactory=new Configuration().configure().buildSessionFactory();
        Session session=sessionFactory.openSession();

        session.beginTransaction();
        Profile p= new Profile();
        p.setProfileName("Srikanth");

        Section s1= new Section();
        s1.setSectionName("Names");
        s1.setProfileId(p);

        Section s2= new Section();
        s2.setProfileId(p);
        s2.setSectionName("Address");

        session.save(p);
//      session.save(s1);
//      session.save(s2);
        session.getTransaction().commit();
        session.close();
    }

}

1 个答案:

答案 0 :(得分:1)

可能在Section类上缺少@Entity。