一、数据库

二、代码

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. Datawarehouse

  2. C++ MFC实现基于RFID读写器的上位机软件

    C++ MFC实现基于RFID读写器的上位机软件 该博客涉及的完整工程托管在https://github.com/Wsine/UpperMonitor,觉得好请给个Star (/▽\=) 运行和测试环 ...

  3. JSP动作学习一

    创建一个简单的模拟登陆功能的网页,没有用数据库技术,只是简单的学习jsp语法 首先创建一个登陆页面 <%@ page language = "java" import=&qu ...

  4. IOS用CGContextRef画各种图形(文字、圆、直线、弧线、矩形、扇形、椭圆、三角形、圆角矩形、贝塞尔曲线、图片)

    ... 首先了解一下CGContextRef: An opaque type that represents a Quartz 2D drawing environment. Graphics Con ...

  5. c编程之排序

    1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<string.h> 4 typedef struct Nod ...

  6. Tomcat性能参数设置

    Tomcat性能参数设置 Tomcat性能参数设置 博客分类: Java LinuxTomcat网络应用多线程Socket 默认参数不适合生产环境使用,因此需要修改一些参数 1.修改启动时内存参数.并 ...

  7. 【Longest Palindromic Substring】cpp

    题目: Given a string S, find the longest palindromic substring in S. You may assume that the maximum l ...

  8. 项目部署之VPN+端口映射

    背景:出差开发项目,需要在客户那里部署基本成型的系统.这套系统需要一个公网的ip地址(一个后台管理系统,使用花生壳提供域名服务.一个公网的tcp server,java io实现),但是客户那里无法提 ...

  9. Binary Indexed Tree 2D 分类: ACM TYPE 2014-09-01 08:40 95人阅读 评论(0) 收藏

    #include <cstdio> #include <cstdlib> #include <climits> #include <cstring> # ...

  10. JS模板引擎 :ArtTemplate (2)

    上一篇初略的介绍了一下javascript中的模板引擎,有兴趣的可以戳 这里 . 这一篇将带着大家一起做一个简易的模板引擎, 上一篇介绍到:模板引擎其实做的就是两件事. 根据一定的规则,解析我们所定义 ...