简体   繁体   English

Hibernate / JPA中的一对多关系

[英]One To Many Relation in Hibernate / JPA

iam new to hibernate,iam using two classes which have person and section but iam able to relate them, iam getting like Exception in thread "main" org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany targeting an unmapped class: in.quadra.apps.Profile.Sections[in.quadra.apps.Section] iam是休眠的新手,iam使用两个具有person和section但可以关联它们的类,iam就像线程“ main” org.hibernate.AnnotationException中的Exception一样:使用@OneToMany或@ManyToMany定位未映射的类:in。 quadra.apps.Profile.Sections [in.quadra.apps.Section]

Person.java 人.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();
    }

}

Section类上可能缺少@Entity。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM