正在嵌入模型类,而不向类指定@Embeddable注释

时间:2015-08-12 08:14:24

标签: java hibernate annotations

我一直在尝试使用hibernate-core-4.3.10.Final.jar来创建一个虚拟项目。我创建了一个模型类UserDetails,它有一个Address字段,实际上是一个可嵌入的对象。在模型类中,我使用@Embedded注释声明了这个字段,但我还没有使用@Embeddable注释将Address类定义为Embeddable。该对象仍然嵌入在UserDeatils实体中。 @Embeddable注释是否可选? @Embedded注释是否足以让hibernate相应地进行映射? 以下是代码片段: -

/** UserDetails Class **/
package com.st.hibernate.models;

import java.util.Date;

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 
@Table(name="USER_DETAILS")
public class UserDetails {
    @Id
    @Column(name="USER_ID")
    @GeneratedValue(strategy=GenerationType.AUTO)
    private int id;
    @Column(name="USER_NAME")
    @Transient
    private String userName;
    @Embedded
    private Address address;


    /**
     * @return the address
     */
    public Address getAddress() {
        return address;
    }
    /**
     * @param address the address to set
     */
    public void setAddress(Address address) {
        this.address = address;
    }
    @Temporal(TemporalType.DATE)
    private Date currentDate;
    @Lob        // Large Objects----> CLob/BLob---->Character/Byte Larger Object
    private String description;
    /**
     * @return the description
     */
    public String getDescription() {
        return description;
    }
    /**
     * @param description the description to set
     */
    public void setDescription(String description) {
        this.description = description;
    }
    /**
     * @return the currentDate
     */
    public Date getCurrentDate() {
        return currentDate;
    }
    /**
     * @param currentDate the currentDate to set
     */
    public void setCurrentDate(Date currentDate) {
        this.currentDate = currentDate;
    }
    /**
     * @return the id
     */
    public int getId() {
        return id;
    }
    /**
     * @param id the id to set
     */
    public void setId(int id) {
        this.id = id;
    }
    /**
     * @return the userName
     */
    public String getUserName() {
        return userName;
    }
    /**
     * @param userName the userName to set
     */
    public void setUserName(String userName) {
        this.userName = userName;
    }

}

和地址类: -

package com.st.hibernate.models;

public class Address {

    private String pincode;
    private String city;
    private String state;
    /**
     * @return the pincode
     */
    public String getPincode() {
        return pincode;
    }
    /**
     * @param pincode the pincode to set
     */
    public void setPincode(String pincode) {
        this.pincode = pincode;
    }
    /**
     * @return the city
     */
    public String getCity() {
        return city;
    }
    /**
     * @param city the city to set
     */
    public void setCity(String city) {
        this.city = city;
    }
    /**
     * @return the state
     */
    public String getState() {
        return state;
    }
    /**
     * @param state the state to set
     */
    public void setState(String state) {
        this.state = state;
    }
}

提前致谢。

0 个答案:

没有答案
相关问题