2012-07-03 182 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。