一、数据库

二、代码

1.

 package org.jpwh.model.simple;

 import javax.persistence.Column;
import javax.persistence.Embeddable;
import javax.validation.constraints.NotNull; /**
*
* Instead of <code>@Entity</code>, this component POJO is marked with <code>@Embeddable</code>. It
* has no identifier property.
*/
@Embeddable
public class Address { @NotNull // Ignored for DDL generation!
@Column(nullable = false) // Used for DDL generation!
protected String street; @NotNull
@Column(nullable = false, length = 5) // Override VARCHAR(255)
protected String zipcode; @NotNull
@Column(nullable = false)
protected String city; /**
* Hibernate will call this no-argument constructor to create an instance, and then
* populate the fields directly.
*/
protected Address() {
} /**
* You can have additional (public) constructors for convenience.
*/
public Address(String street, String zipcode, String city) {
this.street = street;
this.zipcode = zipcode;
this.city = city;
} public String getStreet() {
return street;
} public void setStreet(String street) {
this.street = street;
} public String getZipcode() {
return zipcode;
} public void setZipcode(String zipcode) {
this.zipcode = zipcode;
} public String getCity() {
return city;
} public void setCity(String city) {
this.city = city;
}
}

You should override the equals() and hashCode() methods of Address and compare instances by value.

2.

 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;
} // ...
}

3.

三、

JavaPersistenceWithHibernate第二版笔记-第五章-Mapping value types-002使用@Embeddable的更多相关文章

  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-004嵌套组件的注解AttributeOverrides

    一.数据库 二.代码 1. package org.jpwh.model.advanced; import javax.persistence.AttributeOverride; import ja ...

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

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

  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. Qt---在QLabel上实现系统时间

    参考:http://blog.csdn.net/g457499940/article/details/11923887 ---------------------------------------- ...

  2. JavaScript设计模式与开发实践——JavaScript的多态

    “多态”一词源于希腊文polymorphism,拆开来看是poly(复数)+ morph(形态)+ ism,从字面上我们可以理解为复数形态. 多态的实际含义是:同一操作作用于不同的对象上面,可以产生不 ...

  3. 怎样把php数组转换成字符串,php implode()

    实例代码 一维数组转换成字符串代码! <?php $arr1=array("shu","zhu","1"); $c=implode(& ...

  4. udp 视频包网络传输花屏

    视频数据传输在传输层可以选择TCP或者UDP,TCP面向连接,传输中断,发送端是知道的.TCP传输的好处是不丢包,坏处是网络不太好的情况下会越堵越严重.UDP非面向连接,发送端只管发送数据,接收端有没 ...

  5. Workaround for Markdown blogging platform that to use LaTeX

    Here is a solution to solve the lack of LaTeX support: by using Codecogs API. For instance, if you n ...

  6. 编译器手工开栈(hdu可以其他可以尝试)

    做题的时候经常遇到深度递归的,当然也可以改成非递归形式.如果写成递归形式会爆栈,所以可以用手工扩展栈. C++ (一般用C++提交,所以就推荐这种了) #pragma comment(linker, ...

  7. 单点登录的原理与CAS技术的研究

    1.什么是单点登录? 关于单点登录技术的说明参考文章:http://www.cnblogs.com/yupeng/archive/2012/05/24/2517317.html 一般来说,整个原理大家 ...

  8. 【BZOJ】【3442】学习小组

    网络流/费用流 orz zyf 裸的费用流,根据题目描述即可建出如下的图: S->i 费用表示每有一个加入第 i 个小组的学生,需要花的钱,由于是跟流量(人数)的二次方相关,所以要拆边……然后每 ...

  9. ubuntu 安装完成后的工作

    以安装 ubuntu 15.10 为例 1. 备份并更改源 cd /etc/apt sudo cp source.list source.list.bak sudo vi source.list 删除 ...

  10. Docker学习资料

    官方Docker:https://www.docker.com/ CSDN Docker : http://docker.csdn.net/ Docker中文社区:http://www.docker. ...