
数据库是oracle
以一对多为例:user50一的一方 order50是多的一方
首先是实体类:
这里的实体是双向关系,既通过user50可以找到order50,通过order50可以找到user50
user实体类:
package com.c50.entity; import java.util.Date; import java.util.HashSet; import java.util.Set; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.OneToMany; import javax.persistence.Table; import javax.persistence.Temporal; import javax.persistence.TemporalType; import org.hibernate.annotations.Cascade; import org.hibernate.annotations.CascadeType; import org.hibernate.annotations.GenericGenerator; //代表此类参与ORM映射,此注解必须要有 @Entity //代表user这个类映射了一个表user50,如果表名和类名一样,此注解可以省略 @Table(name="user50") public class User { //主键,此注解必须要有 @Id //数据库表中主键列名为id,如果实体类属性名和列名相同,此注解可以省略 @Column(name="id") //主键的维护策略 @GenericGenerator(name="inc50",strategy="increment") @GeneratedValue(generator="inc50") private Integer userID; private String name; private Integer age; @Column(name="birth") //此属性是日期类型,time:只保存时间 date:只保存日期 timestamp:(默认)日期+时间 @Temporal(TemporalType.DATE) private Date birthday; //关系属性 //mappedBy="对方中映射着外键的关系属性"=="order类中关系属性为user" @OneToMany(mappedBy="user") //ALL:增删改都可以级联(cascade) //save-update:增加和更新可以级联 //delete:删除可以级联 //如果不设置,则增删改都不可以级联 //注意:查询例外,不受级联权限的控制,默认级联。 @Cascade(value=CascadeType.DELETE) private Set<Order> orders=new HashSet<Order>(); public Set<Order> getOrders() { return orders; } public void setOrders(Set<Order> orders) { this.orders = orders; } public User(){} public User(Integer userID, String name, Integer age, Date birthday) { super(); this.userID = userID; this.name = name; this.age = age; this.birthday = birthday; } public Integer getUserID() { return userID; } public void setUserID(Integer userID) { this.userID = userID; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } @Override public String toString() { return "User [userID=" + userID + ", name=" + name + ", age=" + age + ", birthday=" + birthday + "]"; } }
order实体类:
package com.c50.entity; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.Table; import org.hibernate.annotations.GenericGenerator; @Entity @Table(name="order50") public class Order { @Id @GenericGenerator(name="inc50",strategy="increment") @GeneratedValue(generator="inc50") private Integer id; private Double price; private String note; //关系属性 @ManyToOne @JoinColumn(name="user_id") private User user; public User getUser() { return user; } public void setUser(User user) { this.user = user; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public Double getPrice() { return price; } public void setPrice(Double price) { this.price = price; } public String getNote() { return note; } public void setNote(String note) { this.note = note; } @Override public String toString() { return "Order [id=" + id + ", price=" + price + ", note=" + note + "]"; } public Order(){} public Order(Integer id, Double price, String note) { super(); this.id = id; this.price = price; this.note = note; } }
测试代码:
package com.c50.test; import java.util.Date; import java.util.Set; import org.hibernate.Session; import org.hibernate.Transaction; import org.junit.Test; import com.c50.entity.Order; import com.c50.entity.Passport; import com.c50.entity.User; import com.c50.entity.User; import com.c50.util.HibernateUtil; public class TestOne2Many { @Test public void testQuery(){ Session session=HibernateUtil.getCurrentSession(); Transaction tx=session.beginTransaction(); //User user=(User)session.get(User.class,2); Order order=(Order)session.get(Order.class,1); /*for(Order order:user.getOrders()){ System.out.println(order); } System.out.println(user);*/ tx.commit(); } /** * 1.单独插入主表:和单表插入无异。略 * 2.单独插入从表:为已有的主表附加数据 */ @Test public void testInsert(){ Session session=HibernateUtil.getCurrentSession(); Transaction tx=session.beginTransaction(); //查询主表数据 User user=(User)session.get(User.class,1); //创建从表数据 Order order=new Order(null,500.50,"c50"); //维护关系:告知order,其从属哪个user,以获得外键来源 order.setUser(user); //保存 session.save(order); tx.commit(); } /** * 3.同时插入双方:级联插入 */ @Test public void testInsert2(){ Session session=HibernateUtil.getCurrentSession(); Transaction tx=session.beginTransaction(); //创建双方数据 User user=new User(null,"liwenhao",18,new Date()); Order order=new Order(null,300.50,"c50"); //维护关系 user.getOrders().add(order);//使得user可以携带order一起入库保存 order.setUser(user);//使得order有外键来源 //保存 session.save(user); tx.commit(); } /** * 删除 1.输出主表:级联删除 */ @Test public void testDelete(){ Session session=HibernateUtil.getCurrentSession(); Transaction tx=session.beginTransaction(); //查询要删除的主表 User user=(User)session.get(User.class, 3); //删除 session.delete(user); tx.commit(); } /** * 删除 2.单独删除从表:和单表操作无异 */ @Test public void testDelete2(){ Session session=HibernateUtil.getCurrentSession(); Transaction tx=session.beginTransaction(); //查询要删除的从表 Order order=(Order)session.get(Order.class,5); //删除 session.delete(order); tx.commit(); } /** * 更新: * 1.单独更新一方 * 2.级联更新双方 更新时的注意: 如果A不是孤立的表,且,A上明确映射了外键的话,则在更新A时,就不能直接更新new出来的A, 而是应该先查询,改属性,更新(事务内部加载出的数据) */ @Test public void testUpdate(){ Session session=HibernateUtil.getCurrentSession(); Transaction tx=session.beginTransaction(); //1.获取数据 User User=(User)session.get(User.class,6); //2.修改属性 User.setName("cuiyafeng55"); User.setAge(19); //更新 session.update(User); tx.commit(); } }