1

I tried everything, but I'm stuck. Can you help me please?

The small project is just a preliminary test for a large database. I've only been using Hibernate for a week and I'm trying to connect it all with Srping MVC Tomcat later.

But now I just want to be able to recognize the mistakes in the mapping.

Console:

    `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;
    }
}

Thank you for the time invested! :)

2
  • You miss the Annotation about private Address address;. Think it must @oneToOne(....) Commented Sep 7, 2018 at 10:53
  • See vladmihalcea.com/… Commented Sep 7, 2018 at 10:54

1 Answer 1

5

Actually, your mapping is ok. Each Employee could have one Address. But you did small mistake i think. As my knowledge Whenever creating tables in hibernate using annotation use all annotation at one place i mean Field or property level. Basically, Hibernate will look to other required annotation based on @Id annotation position. So in your case @Id is in field level and @OneToOne is on property level in Employee table. Please do the same changes and try again, let me know.

@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 + "}";
    }
Sign up to request clarification or add additional context in comments.

2 Comments

@AngardBansode Thank you so much! it worked. However, I now get a SQL error .. ` Sep 07, 2018 2:28:53 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions WARN: SQL Error: 1364, SQLState: HY000 Sep 07, 2018 2:28:53 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions ERROR: Field 'emp_id' doesn't have a default value ` and also a "could not execute statement" error caused by the SQL
If this solution helps, Could you please accept this solution and upvote.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.