@AttributeOverride不能使用Hibernate 5

时间:2017-02-02 16:40:10

标签: java spring hibernate jpa orm

我使用的是Hibernate 5.2.6版。我有一个实例,我必须使用@AttributeOverrides注释来覆盖我之前与另一个实体一起使用的嵌入列属性的名称。但它似乎并没有起作用。

我正在

  

Hibernate ERROR - 未知列' ADDRESS_LINE_1'在'字段列表

以下是我的代码:

Address.java

  package com.myApps.data.entities;

  import javax.persistence.Column;
  import javax.persistence.Embeddable;

  @Embeddable
  public class Address {


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

     @Column(name="ADDRESS_LINE_2")
     private String addressLine2;

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

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

     @Column(name="ZIP_CODE")
     private String zipCode;

     public Address() {
     }

     public String getAddressLine1() {
        return addressLine1;
     }

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

     public String getAddressLine2() {
        return addressLine2;
     }

     public void setAddressLine2(String addressLine2) {
        this.addressLine2 = addressLine2;
     }

     public String getCity() {
        return city;
     }

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

      public String getState() {
        return state;
     }

     public void setState(String state) {
        this.state = state;
     }

      public String getZipCode() {
         return zipCode;
      }

      public void setZipCode(String zipCode) {
        this.zipCode = zipCode;
      }
  }

User.java

package com.myApps.data.entities;

import java.util.Date;

import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

import org.hibernate.annotations.Formula;

@Entity
@Table(name = "FINANCES_USER")
public class User {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name = "USER_ID")
    private Long userId;

    @Column(name = "FIRST_NAME")
    private String firstName;

    @Column(name = "LAST_NAME")
    private String lastName;

    @Column(name = "BIRTH_DATE")
    private Date birthDate;

    @Column(name = "EMAIL_ADDRESS")
    private String emailAddress;

    @Embedded
    @AttributeOverrides({
            @AttributeOverride(name="addressline1", column=@Column(name="USER_ADDRESS_LINE_1")),
            @AttributeOverride(name="addressline2", column=@Column(name="USER_ADDRESS_LINE_2"))
            })
    private Address address;

    @Column(name = "LAST_UPDATED_DATE")
    private Date lastUpdatedDate;

    @Column(name = "LAST_UPDATED_BY")
    private String lastUpdatedBy;

    @Column(name = "CREATED_DATE", updatable = false)
    private Date createdDate;

    @Column(name = "CREATED_BY", updatable = false)
    private String createdBy;

    @Formula("lower(datediff(curdate(), birth_date)/365)")
    private int age;

    public int getAge() {
        return age;
    }

    public Address getAddress() {
        return address;
    }


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

    public void setAge(int age) {
        this.age = age;
    }

    public Long getUserId() {
        return userId;
    }

    public void setUserId(Long userId) {
        this.userId = userId;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public Date getBirthDate() {
        return birthDate;
    }

    public void setBirthDate(Date birthDate) {
        this.birthDate = birthDate;
    }

    public String getEmailAddress() {
        return emailAddress;
    }

    public void setEmailAddress(String emailAddress) {
        this.emailAddress = emailAddress;
    }

    public Date getLastUpdatedDate() {
        return lastUpdatedDate;
    }

    public void setLastUpdatedDate(Date lastUpdatedDate) {
        this.lastUpdatedDate = lastUpdatedDate;
    }

    public String getLastUpdatedBy() {
        return lastUpdatedBy;
    }

    public void setLastUpdatedBy(String lastUpdatedBy) {
        this.lastUpdatedBy = lastUpdatedBy;
    }

    public Date getCreatedDate() {
        return createdDate;
    }

    public void setCreatedDate(Date createdDate) {
        this.createdDate = createdDate;
    }

    public String getCreatedBy() {
        return createdBy;
    }

    public void setCreatedBy(String createdBy) {
        this.createdBy = createdBy;
    }

}

Application.java

package com.myApps.data;

import java.util.Date;

import org.hibernate.Session;
import org.hibernate.Transaction;

import com.myApps.data.entities.Address;
import com.myApps.data.entities.Bank;
import com.myApps.data.entities.User;

public class Application {

    public static void main(String[] args) {

        Session session = HibernateUtil.getSessionFactory().openSession();

        try {

            Transaction transaction = session.beginTransaction();

            User user = new User();
            Address address = new Address();
            user.setFirstName("Beth");
            user.setLastName("Crimson");
            user.setCreatedBy("Mr Crimson");
            user.setCreatedDate(new Date());
            address.setAddressLine1("22nd street Meadows");
            address.setAddressLine2("House no: 15");
            user.setAddress(address);

            session.save(user);
            transaction.commit();

        } catch (Exception e) {
            // TODO: handle exception
        }

        finally {
            session.close();
        }

    }

}

我正在

  

休眠ERROR - Unknown column 'ADDRESS_LINE_1' in 'field list'

我的代码无法解决任何问题。

请帮我解决这个问题。

1 个答案:

答案 0 :(得分:3)

如果你在@Embeddable中使用camelcase:

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

@Column(name="ADDRESS_LINE_2")
private String addressLine2;

然后你也应该在覆盖中使用它:

@Embedded
    @AttributeOverrides({
            @AttributeOverride(name="addressLine1", column=@Column(name="USER_ADDRESS_LINE_1")),
            @AttributeOverride(name="addressLine2", column=@Column(name="USER_ADDRESS_LINE_2"))
            })
    private Address address;