JavaPersistenceWithHibernate第二版笔记-第五章-Mapping value types-002使用@Embeddable
一、数据库

二、代码
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的更多相关文章
- 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. ...
- 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 ...
- 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 ...
- 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 ...
- JavaPersistenceWithHibernate第二版笔记-第五章-Mapping value types-004嵌套组件的注解AttributeOverrides
一.数据库 二.代码 1. package org.jpwh.model.advanced; import javax.persistence.AttributeOverride; import ja ...
- JavaPersistenceWithHibernate第二版笔记-第五章-Mapping value types-003使用@AttributeOverrides
Each @AttributeOverride for a component property is “complete”: any JPA or Hibernate annotation on t ...
- JavaPersistenceWithHibernate第二版笔记-第四章-Mapping persistent classes-003映射实体时的可选操作(<delimited-identifiers/>、PhysicalNamingStrategy、PhysicalNamingStrategyStandardImpl、、、)
一.自定义映射的表名 1. @Entity @Table(name = "USERS") public class User implements Serializable { / ...
- 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 ...
- JavaPersistenceWithHibernate第二版笔记-第六章-Mapping inheritance-002Table per concrete class with implicit polymorphism(@MappedSuperclass、@AttributeOverride)
一.结构 二.代码 1. package org.jpwh.model.inheritance.mappedsuperclass; import javax.persistence.MappedSup ...
随机推荐
- 关于使用,NI采集卡+labview信号采集,问题交流【第二贴】
*** 采集卡 NI PCI-6534: max sample rate 20MHz:32位的数字输入,数字输出. ***输入信号: 峰峰值,4.16V 最小值 -80mV 频率 ...
- net core 实战之 redis 负载均衡和"高可用"实现
net core 实战之 redis 负载均衡和"高可用"实现 1.概述 分布式系统缓存已经变得不可或缺,本文主要阐述如何实现redis主从复制集群的负载均衡,以及 redis的& ...
- Ant学习---第五节:Ant_Junit介绍(基于3的版本)
Junit3 和 Junit4 有本质上的区别 1.普通java类,代码如下: package learn.junit; public class HelloWorld { public String ...
- loadrunner简单使用——HTTP,WebService,Socket压力测试脚本编写
使用loadrunner进行压力测试主要分两步,第一步是编写脚本(比较重点),第二步执行测试(配置都是在界面上点点就行了,当然我只的是比较简单的,能满足日常需要的),第三步分析结果(这一步比较高深,但 ...
- nodeJs事件之监听移除事件
var EventEmitter=require('events').EventEmitter var life=new EventEmitter(); //comfort 求安慰,函数名: //fo ...
- selenium--下拉列表选择
html 代码: <select name="ColumnId" id="columnOrd"> <option value="2& ...
- NYOJ-32 组合数 AC 分类: NYOJ 2013-12-30 07:42 189人阅读 评论(0) 收藏
#include<stdio.h> int num[100]; int pnum(int n,int v); int mv=0; int main(){ int n,v; scanf(&q ...
- 在JavaScript中判断整型的N种方法
原文:http://www.cnblogs.com/YcYYcY/p/3759184.html 整数类型(Integer)在JavaScript经常会导致一些奇怪的问题.在ECMAScript的规范中 ...
- 从浏览器的console获取angularjs的scope
http://ionicframework.com/blog/angularjs-console/ 1: Access Scopes We can access any scope (even iso ...
- js生成唯一值的函数
利用了js的闭包性质 var uniqueNumber = (( function(){ var value = 0; return function(){ return ++value; }; }) ...