您的位置:首页 > 移动开发

JavaPersistenceWithHibernate第二版笔记-第五章-Mapping value types-003使用@AttributeOverrides

2016-04-07 11:41 393 查看
Each @AttributeOverride for a component property is “complete”: any JPA or Hibernate annotation on the overridden property is ignored. This means the @Column annotations on the Address class are ignored—all BILLING_* columns are NULL able!(Bean Validation still recognizes the @NotNull annotation on the component property, though; Hibernate only overrides persistence annotations.)

1.

package org.jpwh.model.simple;

import org.jpwh.model.Constants;

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.Id;
import javax.persistence.Table;
import java.io.Serializable;
import java.math.BigDecimal;

@Entity
@Table(name = "USERS")
public class User implements Serializable {

@Id
@GeneratedValue(generator = Constants.ID_GENERATOR)
protected Long id;

public Long getId() {
return id;
}

protected String username;

public User() {
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

// The Address is @Embeddable, no annotation needed here...
protected Address homeAddress;

public Address getHomeAddress() {
return homeAddress;
}

public void setHomeAddress(Address homeAddress) {
this.homeAddress = homeAddress;
}

@Embedded // Not necessary...
@AttributeOverrides({
@AttributeOverride(name = "street",
column = @Column(name = "BILLING_STREET")), // NULLable!
@AttributeOverride(name = "zipcode",
column = @Column(name = "BILLING_ZIPCODE", length = 5)),
@AttributeOverride(name = "city",
column = @Column(name = "BILLING_CITY"))
})
protected Address billingAddress;

public Address getBillingAddress() {
return billingAddress;
}

public void setBillingAddress(Address billingAddress) {
this.billingAddress = billingAddress;
}

public BigDecimal calcShippingCosts(Address fromLocation) {
// Empty implementation of business method
return null;
}

// ...
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: