下面是我的代码我想在我的公司类和Person类中实现OnetoMany Hibernate Mapping 代码对我来说似乎正确但是打印数据时输出有错误 如果有人可以帮我解决这个问题,那就太好了。
我希望从公司类到多人的一对多映射,一次打印所有映射数据
package Rajat.Practice1;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.OneToMany;
import javax.persistence.Table;
@Entity
public class Company {
private int c_id;
private String C_name;
public List<Person> person = new ArrayList<Person>();
@Id
@GeneratedValue
@Column(name="c_id")
public int getC_id() {
return c_id;
}
public void setC_id(int c_id) {
this.c_id = c_id;
}
@Column(name="name")
public String getC_name() {
return C_name;
}
public void setC_name(String c_name) {
C_name = c_name;
}
@Override
public String toString() {
return "Company [c_id=" + c_id + ", C_name=" + C_name + ", person=" + person + "]";
}
public List<Person> getPerson() {
return person;
}
@OneToMany(mappedBy="company", fetch=FetchType.EAGER)
public void setPerson(List<Person> person) {
this.person = person;
}
}
公司类
package Rajat.Practice1;
import javax.persistence.Column;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import org.hibernate.annotations.CascadeType;
import org.hibernate.annotations.CollectionId;
import javax.persistence.Entity;
import org.hibernate.annotations.Fetch;
@Entity
public class Person {
private int Pid;
private String name;
public Company company;
private PersonDetail p_detail;
//@OneToOne ( cascade=javax.persistence.CascadeType.ALL, fetch=FetchType.EAGER )
//@JoinColumn(name="pdetail_pk")
public PersonDetail getP_detail() {
return p_detail;
}
public void setP_detail(PersonDetail p_detail) {
this.p_detail = p_detail;
}
@Id
@GeneratedValue
@Column(name="Pid_pk")
public int getPid() {
return Pid;
}
public void setPid(int pid) {
Pid = pid;
}
@Column(name="name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
// @ManyToOne(fetch=FetchType.EAGER)
// @JoinColumn(name="c_id")
public Company getCompany() {
return company;
}
public void setCompany(Company company) {
this.company = company;
}
@Override
public String toString() {
return "Person [Pid=" + Pid + ", name=" + name + ", company=" + company + ", p_detail=" + p_detail + "]";
}
}
人类
package Rajat.Practice1;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.spi.ServiceRegistry;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistryBuilder;
import org.hibernate.tool.hbm2ddl.SchemaExport;
public class Main {
public static void main(String[] args) {
Configuration cfg=new Configuration();
cfg.configure("hibernate.cfg.xml");
SessionFactory sf=cfg.buildSessionFactory();
Session session=sf.openSession();
Transaction tx=session.beginTransaction();
List <Person> lp=new ArrayList<Person>();
PersonDetail pd=new PersonDetail();
pd.setCity("jaipur");
pd.setState("jaipur");
Person p=new Person();
p.setName("Rajat");
Person p1=new Person();
p1.setName("Dynamo");
Company c=new Company();
c.setC_name("Protechgenie");
p.setCompany(c);
p1.setCompany(c);
c.getPerson().add(p);
c.getPerson().add(p1);
lp.add(p);
lp.add(p1);
p.setP_detail(pd);
session.save(c);
session.save(p);
session.save(p1);
session.getTransaction().commit();
Company com=(Company)session.get(Company.class, 1);
Person per=(Person) session.get(Person.class, 1);
System.out.println(com);
}
}
主类
{{1}}
答案 0 :(得分:2)
Company
类中,取消注释映射配置Person
类中,注释(@OneToMany(mappedBy="company", fetch=FetchType.EAGER)
)应该在getter方法上。这是我的代码(注意我删除了一些未使用的属性)
@Entity
public class Company {
private int c_id;
private String C_name;
public List<Person2> person = new ArrayList<Person2>();
@Id
@GeneratedValue
@Column(name="c_id")
public int getC_id() {
return c_id;
}
public void setC_id(int c_id) {
this.c_id = c_id;
}
@Column(name="name")
public String getC_name() {
return C_name;
}
public void setC_name(String c_name) {
C_name = c_name;
}
@Override
public String toString() {
return "Company [c_id=" + c_id + ", C_name=" + C_name + ", person=" + person + "]";
}
@OneToMany(mappedBy="company", fetch=FetchType.EAGER)
public List<Person2> getPerson() {
return person;
}
public void setPerson(List<Person2> person) {
this.person = person;
}
}
@Entity
public class Person2 {
private int Pid;
private String name;
public Company company;
@Id
@GeneratedValue
@Column(name="Pid_pk")
public int getPid() {
return Pid;
}
public void setPid(int pid) {
Pid = pid;
}
@Column(name="name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(name="c_id")
public Company getCompany() {
return company;
}
public void setCompany(Company company) {
this.company = company;
}
@Override
public String toString() {
return "Person [Pid=" + Pid + ", name=" + name + ", company=" + company + "]";
}