一、数据库

二、代码

1.

 package org.jpwh.model.advanced;

 import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
import javax.persistence.Embeddable;
import javax.validation.constraints.NotNull; @Embeddable
public class Address { @NotNull
@Column(nullable = false)
protected String street; @NotNull
@AttributeOverrides(
@AttributeOverride(
name = "name",
column = @Column(name = "CITY", nullable = false)
)
)
protected City city; public String getStreet() {
return street;
} public void setStreet(String street) {
this.street = street;
} public City getCity() {
return city;
} public void setCity(City city) {
this.city = city;
} // ...
}

2.

 package org.jpwh.model.advanced;

 import javax.persistence.Column;
import javax.persistence.Embeddable;
import javax.validation.constraints.NotNull; @Embeddable
public class City { @NotNull
@Column(nullable = false, length = 5) // Override VARCHAR(255)
protected String zipcode; @NotNull
@Column(nullable = false)
protected String name; @NotNull
@Column(nullable = false)
protected String country; public String getZipcode() {
return zipcode;
} public void setZipcode(String zipcode) {
this.zipcode = zipcode;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getCountry() {
return country;
} public void setCountry(String country) {
this.country = country;
} // ...
}

3.

 package org.jpwh.model.advanced;

 import org.jpwh.model.Constants;

 import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table; @Entity
@Table(name = "USERS")
public class User { @Id
@GeneratedValue(generator = Constants.ID_GENERATOR)
protected Long id; public Long getId() {
return id;
} protected Address address; public Address getAddress() {
return address;
} public void setAddress(Address address) {
this.address = address;
}
}

You can declare @AttributeOverride s at any level, as you do for the name property of the City class, mapping it to the CITY column. This can be achieved with either (as shown) an @AttributeOverride in Address or an override in the root entity class,User . Nested properties can be referenced with dot notation: for example, on User#address , @AttributeOveride(name = "city.name") references the Address #City#name attribute.

JavaPersistenceWithHibernate第二版笔记-第五章-Mapping value types-004嵌套组件的注解AttributeOverrides的更多相关文章

  1. JavaPersistenceWithHibernate第二版笔记-第五章-Mapping value types-001Mapping basic properties(@Basic、@Access、access="noop"、@Formula、@ColumnTransformer、@Generated、 @ColumnDefaul、@Temporal、@Enumerated)

    一.简介 在JPA中,默认所有属性都会persist,属性要属于以下3种情况,Hibernate在启动时会报错 1.java基本类型或包装类 2.有注解 @Embedded 3.有实现java.io. ...

  2. JavaPersistenceWithHibernate第二版笔记-第五章-Mapping value types-007UserTypes的用法(@org.hibernate.annotations.Type、@org.hibernate.annotations.TypeDefs、CompositeUserType、DynamicParameterizedType、、、)

    一.结构 二.Hibernate支持的UserTypes接口  UserType —You can transform values by interacting with the plain JD ...

  3. JavaPersistenceWithHibernate第二版笔记-第五章-Mapping value types-006类型转换器( @Converter(autoApply = true) 、type="converter:qualified.ConverterName" )

    一.结构 二.代码 1. package org.jpwh.model.advanced; import java.io.Serializable; import java.math.BigDecim ...

  4. JavaPersistenceWithHibernate第二版笔记-第五章-Mapping value types-005控制类型映射(Nationalized、@LOB、@org.hibernate.annotations.Type)

    一.简介 1. 2. 3. 4. to override this default mapping. The JPA specification has a convenient shortcut a ...

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

    Each @AttributeOverride for a component property is “complete”: any JPA or Hibernate annotation on t ...

  6. JavaPersistenceWithHibernate第二版笔记-第五章-Mapping value types-002使用@Embeddable

    一.数据库 二.代码 1. package org.jpwh.model.simple; import javax.persistence.Column; import javax.persisten ...

  7. JavaPersistenceWithHibernate第二版笔记-第四章-Mapping persistent classes-003映射实体时的可选操作(<delimited-identifiers/>、PhysicalNamingStrategy、PhysicalNamingStrategyStandardImpl、、、)

    一.自定义映射的表名 1. @Entity @Table(name = "USERS") public class User implements Serializable { / ...

  8. JavaPersistenceWithHibernate第二版笔记-第六章-Mapping inheritance-003Table per concrete class with unions(@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)、<union-subclass>)

    一.代码 1. package org.jpwh.model.inheritance.tableperclass; import org.jpwh.model.Constants; import ja ...

  9. JavaPersistenceWithHibernate第二版笔记-第六章-Mapping inheritance-002Table per concrete class with implicit polymorphism(@MappedSuperclass、@AttributeOverride)

    一.结构 二.代码 1. package org.jpwh.model.inheritance.mappedsuperclass; import javax.persistence.MappedSup ...

随机推荐

  1. Java把内存划分为4个部分 1. 代码区 1、栈区 3、堆区 4、静态区域

    1.栈区(stacksegment)—由编译器自动分配释放,存放函数的参数值,局部变量的值等,具体方法执行结束之后,系统自动释放JVM内存资源 2.堆区(heapsegment)—一般由程序员分配释放 ...

  2. [网络配置相关]——netstat命令

    netstat:显示网络状态信息 -a   显示所有连接状态的网络的所有选项-l    仅显示LISTEN状态的连接-n   直接显示IP地址,而不通过域名服务器-p   把进程名和进程PID也显示出 ...

  3. 《实时控制软件设计》Git 基本操作练习

    根据老师提供的教程 对 数据库创建.提交文件.创建分支.删除分支.合并分支.冲突处理等操作进行了练习 得到log文件如下: yanbin-guo@yanbinguo MINGW64 /Git (mas ...

  4. Grails 对象关联映射 (GORM) 一

    转自:http://justjavac.iteye.com/blog/701445 Domain 类是任何商业应用的核心. 他们保存事务处理的状态,也处理预期的行为. 他们通过关联联系在一起, one ...

  5. DFS-HDU 1312 -Red and Black

    D - Red and Black Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

  6. 【bzoj1008】[HNOI2008]越狱

    1008: [HNOI2008]越狱 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 7692  Solved: 3296[Submit][Status] ...

  7. struct {0}初始化

    #include <iostream> struct MyStruct { MyStruct(int a) { a = b = ; } int a; int b; }; int main( ...

  8. 简单的表视图UITableView

    1.建一个Single View application 2.在故事板中放置一个Table View控件 3.在.h文件中加入协议 <UITableViewDataSource,UITableV ...

  9. Tomcat漏洞说明与安全加固

    Tomcat是Apache软件基金会的一个免费的.开放源码的WEB应用服务器,可以运行在Linux和Windows等多个平台上,由于其性能稳定.扩展性好.免费等特点深受广大用户的喜爱.目前,互联网上绝 ...

  10. Linux 配置网络

    1.vi  /etc/sysconfig/network-scripts/ifcfg-eth0 2. # Advanced Micro Devices [AMD] 79c970 [PCnet32 LA ...