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

一、数据库

二、代码

1.

 1 package org.jpwh.model.advanced;
 2
 3 import javax.persistence.AttributeOverride;
 4 import javax.persistence.AttributeOverrides;
 5 import javax.persistence.Column;
 6 import javax.persistence.Embeddable;
 7 import javax.validation.constraints.NotNull;
 8
 9 @Embeddable
10 public class Address {
11
12     @NotNull
13     @Column(nullable = false)
14     protected String street;
15
16     @NotNull
17     @AttributeOverrides(
18         @AttributeOverride(
19             name = "name",
20             column = @Column(name = "CITY", nullable = false)
21         )
22     )
23     protected City city;
24
25     public String getStreet() {
26         return street;
27     }
28
29     public void setStreet(String street) {
30         this.street = street;
31     }
32
33     public City getCity() {
34         return city;
35     }
36
37     public void setCity(City city) {
38         this.city = city;
39     }
40
41     // ...
42 }

2.

 1 package org.jpwh.model.advanced;
 2
 3 import javax.persistence.Column;
 4 import javax.persistence.Embeddable;
 5 import javax.validation.constraints.NotNull;
 6
 7 @Embeddable
 8 public class City {
 9
10     @NotNull
11     @Column(nullable = false, length = 5) // Override VARCHAR(255)
12     protected String zipcode;
13
14     @NotNull
15     @Column(nullable =  false)
16     protected String name;
17
18     @NotNull
19     @Column(nullable =  false)
20     protected String country;
21
22     public String getZipcode() {
23         return zipcode;
24     }
25
26     public void setZipcode(String zipcode) {
27         this.zipcode = zipcode;
28     }
29
30     public String getName() {
31         return name;
32     }
33
34     public void setName(String name) {
35         this.name = name;
36     }
37
38     public String getCountry() {
39         return country;
40     }
41
42     public void setCountry(String country) {
43         this.country = country;
44     }
45
46     // ...
47 }

3.

 1 package org.jpwh.model.advanced;
 2
 3 import org.jpwh.model.Constants;
 4
 5 import javax.persistence.Entity;
 6 import javax.persistence.GeneratedValue;
 7 import javax.persistence.Id;
 8 import javax.persistence.Table;
 9
10 @Entity
11 @Table(name = "USERS")
12 public class User {
13
14     @Id
15     @GeneratedValue(generator = Constants.ID_GENERATOR)
16     protected Long id;
17
18     public Long getId() {
19         return id;
20     }
21
22     protected Address address;
23
24     public Address getAddress() {
25         return address;
26     }
27
28     public void setAddress(Address address) {
29         this.address = address;
30     }
31 }

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.

时间: 2024-12-12 04:40:13

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

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

一.自定义映射的表名 1. 1 @Entity 2 @Table(name = "USERS") 3 public class User implements Serializable { 4 // ... 5 } 2.用定界符 1 //@Table(name = "`USER`")的标准 2 @Table(name = "`USER`") 3 4 //JPA的标准 5 @Table(name = "\"USER\"

JavaPersistenceWithHibernate第二版笔记-第六章-Mapping inheritance-004Table per class hierarchy(@Inheritance..SINGLE_TABLE)、@DiscriminatorColumn、@DiscriminatorValue、@DiscriminatorFormula)

一.结构 You can map an entire class hierarchy to a single table. This table includes columns for all properties of all classes in the hierarchy. The value of an extra type discriminator column or formula identifies the concrete subclass represented by a

JavaPersistenceWithHibernate第二版笔记-第四章-Mapping persistent classes-001区分entities and value types

一.介绍 1.这种引用方式不对,但删除时不能级联 要这种引用方式 2.The Bid class could be a problem. In object-oriented modeling, this is marked as a composition (the association between Item and Bid with the diamond). Thus, an Item is the owner of its Bid instances and holds a col

JavaPersistenceWithHibernate第二版笔记-第四章-Mapping persistent classes-002identity详解

一.简介 1.You now have three methods for distinguishing references: ? Objects are identical if they occupy the same memory location in the JVM . This can be checked with the a == b operator. This concept is known as object identity.? Objects are equal i

JavaPersistenceWithHibernate第二版笔记-第七章-004Mapping a map(@MapKeyEnumerated 、 @MapKeyTemporal、@MapKeyColumn)

一.结构 二.代码 1. 1 package org.jpwh.model.collections.mapofstrings; 2 3 import org.jpwh.model.Constants; 4 5 import javax.persistence.CollectionTable; 6 import javax.persistence.Column; 7 import javax.persistence.ElementCollection; 8 import javax.persist

JavaPersistenceWithHibernate第二版笔记-第七章-003Mapping an identifier bag(@OrderColumn、@ElementCollection、@CollectionTable、、)

一.结构 二.代码 1. 1 package org.jpwh.model.collections.listofstrings; 2 3 import org.jpwh.model.Constants; 4 5 import javax.persistence.CollectionTable; 6 import javax.persistence.Column; 7 import javax.persistence.ElementCollection; 8 import javax.persis

JavaPersistenceWithHibernate第二版笔记-第七章-001Mapping a set(@ElementCollection、@CollectionTable、@JoinColumn、)

一.结构 二.代码 1. 1 package org.jpwh.model.collections.setofstrings; 2 3 import org.jpwh.model.Constants; 4 5 import javax.persistence.CollectionTable; 6 import javax.persistence.Column; 7 import javax.persistence.ElementCollection; 8 import javax.persist

Python核心编程(第二版) 第五章习题答案

5-1.整型.讲讲Python普通整型和长整型的区别. 答:Python 的标准整数类型是最通用的数字类型.在大多数 32 位机器上,标准整数类型的取值范围是-2**31到 2**31-1,也就是-2,147,483,648 到 2,147,483,647.如果在 64 位机器上使用 64 位编译器编译 Python,那么在这个系统上的整数将是 64 位. Python 的长整数类型能表达的数值仅仅与你的机器支持的(虚拟)内存大小有关. 5-2.操作符.(a)写一个函数,计算并返回两个数的乘积.

读书笔记--《Python基础教程第二版》-- 第五章 条件、循环和其他语句

5.1 print和import的更多信息 5.1.1 使用独号输出 >>> print 'Age:',42 Age: 42 >>> 1,2,3 (1, 2, 3) >>> print 1,2,3 1 2 3 >>> print (1,2,3) (1, 2, 3) >>> name='Gumby' >>> greeting='Hello' >>> salutation='Mr.'