Java Hibernate attributeoverride注释不起作用?

时间:2017-07-12 20:26:58

标签: java database hibernate

您好我正在学习Hibernate并且我遇到了一个问题,我试图将列名从地址覆盖到家庭地址和办公室地址。我注释掉了所有办公地址代码,但数据库中的列仍然是" CITY_NAME"," STREET_NAME"等等。

有人可以解释一下,谢谢。

Address.java

package org.zm.javabrain.dto;

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

@Embeddable
public class Address {
    @Column(name="STREET_NAME")
    private String stree;
    @Column(name="CITY_NAME")
    private String city;
    @Column(name="STATE_NAME")
    private String state;
    @Column(name="ZIP_NAME")
    private String zip;
    public String getStree() {
        return stree;
    }
    public void setStree(String stree) {
        this.stree = stree;
    }
    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 getZip() {
        return zip;
    }
    public void setZip(String zip) {
        this.zip = zip;
    }


}

这是UserDetails.java

package org.zm.javabrain.dto;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Basic;
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.Lob;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

@Entity // change the name of the entity
@Table(name="USER_DETAILS") // change the name of the table
public class UserDetails implements Serializable {

    @Id @GeneratedValue(strategy=GenerationType.AUTO)
    private int userId;
    private String username;
    private Date joinedDate;
    private String description;

    @Embedded
    @AttributeOverrides({
    @AttributeOverride(name="street",column=@Column(name="HOME_STREET_NAME")),
    @AttributeOverride(name="city",column=@Column(name="HOME_CITY_NAME")),
    @AttributeOverride(name="state",column=@Column(name="HOME_STATE_NAME")),
    @AttributeOverride(name="zip",column=@Column(name="HOME_ZIP_NAME"))})   
    private Address homeAddress;

//  @Embedded
//  @AttributeOverrides({
//      @AttributeOverride(name="street",column=@Column(name="OFFICE_STREET_NAME")),
//      @AttributeOverride(name="city",column=@Column(name="OFFICE_CITY_NAME")),
//      @AttributeOverride(name="state",column=@Column(name="OFFICE_STATE_NAME")),
//      @AttributeOverride(name="zip",column=@Column(name="OFFICE_ZIP_NAME"))})
//  private Address officeAddress;
//  
//  public Address getOfficeAddress() {
//      return officeAddress;
//  }
//  public void setOfficeAddress(Address officeAddress) {
//      this.officeAddress = officeAddress;
//  }
    public Date getJoinedDate() {
        return joinedDate;
    }
    public void setJoinedDate(Date joinedDate) {
        this.joinedDate = joinedDate;
    }

    public Address getHomeAddress() {
        return homeAddress;
    }
    public void setHomeAddress(Address homeAddress) {
        this.homeAddress = homeAddress;
    }
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
    public int getUserId() {
        return userId;
    }
    public void setUserId(int userId) {
        this.userId = userId;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }

}

这是驱动程序类

package org.zm.hibernate;

import java.util.Date;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.zm.javabrain.dto.Address;
import org.zm.javabrain.dto.UserDetails;

public class HibernateTest {

    public static void main(String[] args) {

        UserDetails user = new UserDetails();

        Address addr = new Address();
        addr.setCity("chicago");
        addr.setState("IL");
        addr.setStree("Michigen Ave");
        addr.setZip("55414");

        Address officeAddr = new Address();
        officeAddr.setCity("minneapolis");
        officeAddr.setState("Washington Ave");
        officeAddr.setState("MN");
        officeAddr.setZip("55455");

        user.setUsername("11111");
        user.setHomeAddress(addr);
//      user.setOfficeAddress(officeAddr);
        user.setJoinedDate(new Date());
        user.setDescription("this is a description");

        SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
        Session session = sessionFactory.openSession();
        session.beginTransaction();
        session.save(user);
        session.getTransaction().commit();
        session.close();
    }
}

1 个答案:

答案 0 :(得分:0)

我不能告诉你Hibernate Session是否支持AttributeOverride功能。问题是,我想,Hibernate正在混合两种不同的技术,试图解决同样的问题,这对于试图学习的新用户来说很困惑。 Hibernate可以用作

  • 旧版(原生)ORM和
  • JPA(Java持久性API)实现

正如我从您发布的代码示例中看到的那样,您正在使用JPA注释。包结构为javax.persistence的所有注释,类和属性都是JPA特定的。所以我的建议是,使用Hibernate方式或JPA方式配置持久性,不要混用。

如果要以JPA方式映射实体,请执行以下操作:

  • 将配置信息放在persistence.xml文件中,而不是hibernate.cfg.xml中。该文件应位于源目录的META-INF文件夹下。如果您使用的是Maven,请将其放在src/main/resources/META-INF目录下。该文件应如下所示:

    <persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
         http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
         version="2.1">
    
        <persistence-unit name="yourPU" transaction-type="RESOURCE_LOCAL">
            <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
            <class>org.zm.javabrain.dto.UserDetails</class>
    
            <properties>
                <property name="javax.persistence.jdbc.driver" value="..." />
                <property name="javax.persistence.jdbc.url" value="..." />
                <property name="javax.persistence.jdbc.user" value="..." />
                <property name="javax.persistence.jdbc.password" value="..." />
    
                <property name="hibernate.show_sql" value="true" />
                <property name="hibernate.hbm2ddl.auto" value="create-drop" />
            </properties>
    
        </persistence-unit>
    
     </persistence>
    

用正确的数据库信息替换...

  • 在您的测试类(HibernateTest)中,执行以下操作而不是SessionFactory / Session

    EntityManagerFactory emf = Persistence.createEntityManagerFactory("yourPU");
    EntityManager em = emf.createEntityManager();
    em.getTransaction().begin();
    em.persist(user);
    em.getTransaction().commit;
    em.close();
    emf.close();
    

    您还可以阅读:

  • Hibernate User Guide

  • Java Persistence API Tutorials
  • JPA 2.1 Specification

希望它有所帮助。