gpt4 book ai didi

java - Hibernate/JPA 中的一对多关系

转载 作者:太空宇宙 更新时间:2023-11-04 08:10:28 24 4
gpt4 key购买 nike

iam是hibernate新手,iam使用两个具有person和section但iam能够将它们关联起来的类,iam在线程“main”org.hibernate.AnnotationException中遇到异常:使用@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();
}

}

最佳答案

Section 类上可能缺少 @Entity。

关于java - Hibernate/JPA 中的一对多关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11308181/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com