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)

 

 

 

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 属猴的人养猫怎么办 对方电话拉黑我怎么办 手机号码换了淘宝账号怎么办 扣扣号有密码无法加好友怎么办 扣扣帐号忘记了怎么办 扣扣帐号被冻结怎么办 xp忘了开机密码怎么办 手机怎样知道好友qq密码怎么办 找回微信密码申诉失败怎么办 若微信号被盗钱怎么办 微信号被倒了怎么办 微信支付密码忘了怎么办 微信钱包密码忘了怎么办 忘记qq锁屏密码怎么办 微信里保密柜密码忘记了怎么办 qq漫游记录密码忘了怎么办 当你很烦的时候怎么办 当你烦的时候怎么办 面对刁蛮不讲理的老婆怎么办 老是想以前的事怎么办 被心机婊陷害了怎么办 分手了还被骚扰怎么办 苹果手机屏幕出现冷暖屏怎么办 我感觉媳妇不漂亮怎么办 90后赚不到钱怎么办 处对象感觉好累怎么办 谈朋友感觉好累怎么办 家庭条件不好娶老婆难办怎么办? 异地恋闹矛盾了怎么办 有人威胁要杀我全家怎么办 分手了借我的钱怎么办 脸打架打肿了怎么办 人家不加我qq好友怎么办 一个好友被删了怎么办 dnf脸黑怎么办还有办法 我想登别人微信怎么办 昌硕工资没到怎么办 昌硕离职不批怎么办 昌硕工资不到卡怎么办 上海人去苏州工作社保怎么办 娶个个脾气暴躁的媳妇怎么办