一、数据库

二、代码

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. php捕获网络页面

    <?php $url = 'http://jwzx.cqupt.edu.cn/pubYxKebiao.php?type=zy&yx=06'; $html = file_get_conte ...

  2. Jsp实现form的file和text传递(multipart/form-data)

    Jsp实现form的file和text传递(multipart/form-data) 首先是form部分,因为要有<input type="file" />的类型,所以 ...

  3. 编译QT时出现lib/libQtGui.so: undefined reference to `ts_read_raw'的解决办法

    lib/libQtGui.so: undefined reference to `ts_read_raw' /lib/libQtGui.so: undefined reference to `ts_o ...

  4. [无人值守安装操作系统]__RHEL6__FTP+TFTP+DHCP+Kickstart+PXE

    实验环境 1.实验平台:VMware Workstation 10 2.实验OS:RHEL6 3.服务器A: (1) 10.0.10.158 (2) DHCP/FTP/TFTP (3) 有可使用的yu ...

  5. TList,TObjectList 使用——资源释放

    TOjectList = Class (Tlist); TOjectList继承Tlist,从名字上看就可以知道它是专门为对象列表制作的,那么他到底丰富了那些功能呢? 首先是 TObject 作为对象 ...

  6. Think in java备忘录

    1..new在内部类中的使用 .new可以用使用外部类对象创建一个内部类,对象 DotNew.java package com.gxf.innerclass; public class DotNew ...

  7. ACK

    ACK (Acknowledgement),即确认字符,在数据通信中,接收站发给发送站的一种传输类控制字符.表示发来的数据已确认接收无误. 目录 1基本介绍 2详细释义     1基本介绍编辑 英文缩 ...

  8. selenium IDE--录制和回放脚本

    1 selenium IDE--录制脚本 准备工作:firefox 浏览器安装了selenium IDE 插件 实例:打开百度搜索“软件测试” firefox浏览器打开网址:https://www.b ...

  9. matrix_world_final_2011

    C  http://acm.hust.edu.cn/vjudge/contest/view.action?cid=98613#problem/C 题意:输入16进制的n*m矩阵,其在二进制表示下有6种 ...

  10. 客户端发包 GS端接收

    客户端发包,GS接收 bool GameServer::ProcessLoop(packet& rPkt)//GS线程做的 { if(false == m_spDataLayer->Re ...