Вот мой код, приведенный ниже. Я хотел бы получить OnetoMany Hibernate Mapping в классе моей компании, и код класса Person мне кажется правильным, но при выводе данных есть ошибка при выводе данных.
Я бы хотел, чтобы сопоставление «Один ко многим» из класса компании с несколькими сотрудниками и печать всех сопоставленных данных за один раз
Exception in thread "main" org.hibernate.MappingException: Could not determine type for: java.util.List, at table: Company, for columns: [org.hibernate.mapping.Column(person)]
at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:336)
at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:310)
at org.hibernate.mapping.Property.isValid(Property.java:241)
at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:496)
at org.hibernate.mapping.RootClass.validate(RootClass.java:270)
at org.hibernate.cfg.Configuration.validate(Configuration.java:1358)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1849)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1928)
at Rajat.Practice1.Main.main(Main.java:24)
Класс компании
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);
}
}