Hibernate Tutorial 11 - Configuring Collections and Adding Keys --Continued
来源:互联网 发布:less.js 1.7下载 编辑:程序博客网 时间:2024/05/22 05:33
Configuring Collections and Adding Keys---ended@T19
@you need to use the collections that has index which set collection does not fit here;
@jointable(name="USER_ADDRESS")
@jointable(name="USER_ADDRESS", joinColumns=@JoioColumn(name="USERID")
import org.hibernate.annotations.CollectionOd
import org.hibernate.annotations.GenericGenerator
import org.hibernate.annotations.Type
//which is not JPA anotation specification standard and provided by Hibernate itself;
@Generic Generator(name="hilo-gen", stragedy="hilo")
@CollectionId(columns={@Column="ADDRESS_ID"}, generator = "hilo ", type= @Type(type==long))Hibernate
private Collection<Address> ListOfAddress =new ArrayList<Address>();
Proxy Objects and Eager and Lazy Fetch Types:
@ElementCollection(fetch=FetchType.EAGER);
By default, It is the lazy Fetch;
The Fetch strategy is due to the performance consideration;
One To One Mapping
Since this tutorial, we will work on entity inside an entiry which is different from a valuetype(Previous ListOfAddress type) in an entity
One to One mapping means one entity contains another entity;
public class UserDetails
{
private int userId;
private String userName;
@OneToOne
@JoinColumn(name="Vehicle_ID")
private Vehicle vehicle;
}
One To Many Mapping
A new user_detail_vehicle table will be created for the mapping between user_detail and vehicle to record the mapping information;
public class UserDetails
{
@OneToMany
@JoinTable(name="USER_VEHICLE "joinCilumns=@JoinColumn(name="USER_ID), linverseJoinColumn=@JoinColumn(name=VEHICLE_ID)""Vehicle_ID"))
private int userId;
private String userName;
private Collection<Vehicle> vehicle = new ArrayList<Vehicle>();
}
Many To One Mapping
public class Vehicle
{
@Id @GeneratoedValue
private int vehicle_id;
@ManytoOne
Private UserDetails user;
}
mappedBy and Many To Many Mapping:
we do not have seperate table for mappiing vehicle to user_details;
two relationship tables will be created for manttomany relationship;
public class UserDetails
{
@OneToMany(mappedBy="user")
private int userId;
private String userName;
private Collection<Vehicle> vehicle = new ArrayList<Vehicle>(); //for one to many and many to one and one to one, there is only one collection for vehicle and user_details;
}
public class Vehicle
{
@Id @GeneratoedValue
private int vehicle_id;
@ManytoMany
@JoinColumn(name="USER_ID") // done the mapping within the vehicle using user_id which does not need to create another join table(!@JoinTable)
Private UserDetails user;
private Collection<UserDetail> userDetails userList = new ArrayList();
}
CascadeTypes and Hibernate Collections
@NotFound(action=NotFoundAction.IGNORE)
Hibernate Collections
Bag semantic- List/ArrayyList
Bag semantic with ID- List/ArrayyList
List semantic- List/ArrayyList
Set semantic- Set
Map semantic- Map
Cascade
public class UserDetails
{
@OneToMany(cascade=CascadeType.PERSIST(also could be all or remove))// which will automatically save the realted references entities(sess.save(userdetails;session.save(vehicle2)) will be changed to be as session.persist(user))
private Collection<Vehicle> vehicle = new ArrayList<Vehicle>(); //for one to many and many to one and one to one, there is only one collection for vehicle and private \
Implementing Inheritance:
To take the advantage of the java polyphorsm feature, for e..g., the userdetail class composes/embed a vehicle instance variable which has two subclass boat and car. so Hibernate needs to be aware of the inheritence relateship between entity classes;
By default, it is singble table strategy;
@Inheritance(stratigy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn{ name="VEHICLE_TYPE",discriminatorType=DiscriminatorType.STRING}
@DiscriminatorValue("BIKE")
Implementing Inheritance With Table Per Class:
//multi tables will be created for each class
@Inheritance(stratigy=InheritanceType.TABLE_PER_CLASS)
- Hibernate Tutorial 11 - Configuring Collections and Adding Keys --Continued
- Adding Primary Keys and Foreign Keys to a Table
- op06-adding and configuring network card in VMware
- AVFoundation Tutorial: Adding Overlays and Animations to Videos
- Spring, Hibernate, Maven, and Struts2 integration tutorial
- Sticky Keys, Toggle Keys and Filter Keys
- Partitioning Keys, Primary Keys, and Unique Keys
- CMake Tutorial(5)——Adding a Generated File and Generator
- Java tutorial - collections
- Installing and configuring Xen
- Installing and Configuring ODBC
- Installing and Configuring ModSecurity
- Configuring Servers and Machines
- Alicia Keys and NGB
- Hibernate Tutorial
- Hibernate Tutorial
- Public Keys, Private Keys, and Certificates
- Android Applications Tutorial 15. Configuring Rotation
- C语言 杭电POJ 1090 A+B for Input-Output Practice (II)
- 网站推广、网站优化和SEO的区别
- 指向结构体数组的指针
- 六、统计用户登录的次数
- 二级联动城市选择
- Hibernate Tutorial 11 - Configuring Collections and Adding Keys --Continued
- nyoj-844-A+B Problem(V)
- 第9周项目1--分段函数的求值(解法②)
- C语言 杭电POJ 1091 A+B for Input-Output Practice (III)
- uva 10183 - How Many Fibs?(斐波那契数)
- magento 安装时 Database server does not support the InnoDB storage engine 的解决办法
- 老年群体却无人注意他们的消费能力
- android开发环境搭建
- ASP.NET 4.0在IIS6原生支持没有后缀名的URL请求