休眠-映射:无法确定#的类型,在表:#,对于列:#

时间:2018-09-07 10:46:59

标签: java database hibernate mapping hibernate-mapping

我尝试了所有操作,但被卡住了。 你能帮我吗?

小型项目只是大型数据库的初步测试。我只使用了一个星期的Hibernate,后来我试图将其与Srping MVC Tomcat连接起来。

但是现在我只想能够识别映射中的错误。

控制台:

    `Initial SessionFactory creation failed.org.hibernate.MappingException: Could not determine type for: hiber.data_examplecompany.Address, at table: EMPLOYEE, for columns: [org.hibernate.mapping.Column(address)]
Exception in thread "main" java.lang.ExceptionInInitializerError
    at tools.hiber.HiberUtil.<clinit>(HiberUtil.java:19)
    at tools.hiber.HiberSave.main(HiberSave.java:15)
Caused by: org.hibernate.MappingException: Could not determine type for: hiber.data_examplecompany.Address, at table: EMPLOYEE, for columns: [org.hibernate.mapping.Column(address)]
    at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:486)
    at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:453)
    at org.hibernate.mapping.Property.isValid(Property.java:226)
    at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:624)
    at org.hibernate.mapping.RootClass.validate(RootClass.java:267)
    at org.hibernate.boot.internal.MetadataImpl.validate(MetadataImpl.java:347)
    at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:466)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:708)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:724)
    at tools.hiber.HiberUtil.<clinit>(HiberUtil.java:15)
    ... 1 more

Employee.java:

package hiber.data_examplecompany;

import javax.persistence.Access;
import javax.persistence.AccessType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Table;

import org.hibernate.annotations.Cascade;

@Entity
@Table(name = "EMPLOYEE")
@Access(value=AccessType.FIELD)
public class Employee {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "emp_id")
    private long id;
    @Column(name = "emp_name")
    private String name;
    @Column(name = "emp_salary")
    private double salary;

    private Address address;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    @OneToOne(mappedBy = "employee")
    @Cascade(value = org.hibernate.annotations.CascadeType.ALL)
    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    @Column(name = "emp_name")
    public String getName() {
        System.out.println("Employee getName called");
        return name;
    }

    public void setName(String name) {
        System.out.println("Employee setName called");
        this.name = name;
    }

    @Column(name = "emp_salary")
    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }

    @Override
    public String toString() {
        return "Id= " + id + ", Name= " + name + ", Salary= " + salary
                + ", {Address= " + address + "}";
    }

}

Address.java:

package hiber.data_examplecompany;

import javax.persistence.Access;
import javax.persistence.AccessType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;

import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Parameter;

@Entity
@Table(name = "ADDRESS")
@Access(value=AccessType.FIELD)
public class Address {

    @Id
    @Column(name = "emp_id", unique = true, nullable = false)
    @GeneratedValue(generator = "gen")
    @GenericGenerator(name = "gen", strategy = "foreign", parameters = { @Parameter(name = "property", value = "employee") })
    private long id;

    @Column(name = "address_line1")
    private String addressLine1;

    @Column(name = "zipcode")
    private String zipcode;

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

    @OneToOne
    @PrimaryKeyJoinColumn
    private Employee employee;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getAddressLine1() {
        return addressLine1;
    }

    public void setAddressLine1(String addressLine1) {
        this.addressLine1 = addressLine1;
    }

    public String getZipcode() {
        return zipcode;
    }

    public void setZipcode(String zipcode) {
        this.zipcode = zipcode;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public Employee getEmployee() {
        return employee;
    }

    public void setEmployee(Employee employee) {
        this.employee = employee;
    }

    @Override
    public String toString() {
        return "AddressLine1= " + addressLine1 + ", City=" + city
                + ", Zipcode=" + zipcode;
    }
}

谢谢 投入的时间! :)

1 个答案:

答案 0 :(得分:0)

实际上,您的映射可以。每个员工可以有一个地址。但是我想你犯了小错误。据我所知,每当使用注释在休眠模式下创建表时,将所有注释都放在一个位置,即表示字段或属性级别。基本上,Hibernate将基于@Id注释位置查找其他必需的注释。因此,在您的情况下,Employee表中的@Id位于字段级别,而@OneToOne位于属性级别。请进行相同的更改,然后重试,让我知道。

@Entity
@Table(name = "EMPLOYEE")
@Access(value=AccessType.FIELD)
public class Employee {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "emp_id")
    private long id;
    @Column(name = "emp_name")
    private String name;
    @Column(name = "emp_salary")
    private double salary;

     @OneToOne(mappedBy = "employee" , cascade = CascadeType.ALL)   
    private Address address;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }


    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }


    public String getName() {
        System.out.println("Employee getName called");
        return name;
    }

    public void setName(String name) {
        System.out.println("Employee setName called");
        this.name = name;
    }


    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }

    @Override
    public String toString() {
        return "Id= " + id + ", Name= " + name + ", Salary= " + salary
                + ", {Address= " + address + "}";
    }