2012-07-03 56 views
1

iam新增hibernate,iam使用两个类,它们具有person和section,但是iam能够将它们关联起来,iam在线程“main”中获取像异常org.hibernate.AnnotationException:使用@一对多或@ManyToMany靶向未映射的类:in.quadra.apps.Profile.Sections [in.quadra.apps.Section]Hibernate/JPA中的一对多关系

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(); 
    } 

} 
+0

你需要向你展示科类或我们不能告诉什么是错 –

+1

你应该修复代码,你的人,Java和您的Section.java都定义了公共类资料,不能正确。另外:你想定义什么关系? – wallenborn

回答

1

可能缺少Section类@Entity。