hibernate中多对一关系出错

时间:2018-04-24 11:45:54

标签: hibernate hibernate-mapping

在这里,我想在公司@One与许多人(员工)之间建立关系 和员工@Many到一个(公司) 和@one到员工(人)和地址(Persondetail)之间的一个关系。

我在执行“我的主要课程”时面临以下错误

**解决此错误并让我编码打印有关员工(人)的所有数据,包括其他两个表记录**

  

信息:HHH000400:使用方言:org.hibernate.dialect.MySQL5Dialect       线程“main”中的异常org.hibernate.MappingException:无法确定类型:Rajat.Practice1.Company,在表:Person,for   列:[org.hibernate.mapping.Column(company)]         在org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:336)         在org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:310)         在org.hibernate.mapping.Property.isValid(Property.java:241)         在org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:496)         在org.hibernate.mapping.RootClass.validate(RootClass.java:270)         在org.hibernate.cfg.Configuration.validate(Configuration.java:1358)         在org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1849)         在org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1928)         在Rajat.Practice1.Main.main(Main.java:21)

((MAIN CLASS))

Array<T>

公司课程

T[]

员工(人)班

package Rajat.Practice1;

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

                PersonDetail pd=new  PersonDetail();
                pd.setCity("jaipur");
                pd.setState("jaipur");

                Company c=new Company();
                c.setC_name("Protechgenie");


            Person p=new Person();
            p.setName("Rajat");

            Person p1=new Person();
            p1.setName("Dynamo");

            p.setCompany(c);
            p1.setCompany(c);



            p.setP_detail(pd);

            session.save(c);
            session.save(p);
            session.save(p1);

            session.getTransaction().commit();

        }

    }

员工地址(人员详细信息类)`

    package Rajat.Practice1;

    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.OneToMany;
    import javax.persistence.Table;

    @Entity
    @Table(name="company")
    public class Company {

        @Id
        @GeneratedValue
        @Column(name="c_id")
        private int c_id;
        private String C_name;
        @ElementCollection(targetClass=Person.class)
        private List <Person> person;

        @OneToMany(targetEntity=Person.class, mappedBy="person" , cascade=javax.persistence.CascadeType.ALL, fetch=FetchType.EAGER)
        public List<Person> getPerson() {
            return person;
        }
        public void setPerson(List<Person> person) {
            this.person = person;
        }
        public int getC_id() {
            return c_id;
        }
        public void setC_id(int c_id) {
            this.c_id = c_id;
        }
        public String getC_name() {
            return C_name;
        }
        public void setC_name(String c_name) {
            C_name = c_name;
        }


    }

Hibernate REalations

1 个答案:

答案 0 :(得分:0)

我尝试在下面逐一纠正解决问题的类定义

@Entity
public class Company {

    private int c_id;
    private String name;
    //@ElementCollection(targetClass=Person.class)

    private List<Person> person = new ArrayList<Person>();


    @OneToMany(mappedBy="company",fetch = FetchType.LAZY)
    public List<Person> getPerson() {
        return person;
    }
    public void setPerson(List<Person> person) {
        this.person = 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 getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

人类

@Entity
public class Person {

    private int Pid;

    private String name;

    private 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.LAZY)
    @JoinColumn(name="c_id", nullable = false)
    public Company getCompany() {
        return company;
    }

    public void setCompany(Company company) {
        this.company = company;
    }

}

PersonDetail类

@Entity
@Table(name="pdetail")
public class PersonDetail {

    @Id
    @GeneratedValue
    @Column(name="pdetail_pk")
    private int p_id;


    @Column(name="city")
    private String City;


    @Column(name="state")
    private String State;



    public String getCity() {
        return City;
    }
    public void setCity(String city) {
        City = city;
    }
    public String getState() {
        return State;
    }
    public void setState(String state) {
        State = state;
    }

}