首页 > 代码库 > Hibernate级联关系OneToMany之 Annotation实现
Hibernate级联关系OneToMany之 Annotation实现
[java] view plaincopy
- package com.lwp.hibernate.Model;
- import java.io.Serializable;
- import javax.persistence.CascadeType;
- import javax.persistence.Column;
- import javax.persistence.Entity;
- import javax.persistence.FetchType;
- import javax.persistence.GeneratedValue;
- import javax.persistence.Id;
- import javax.persistence.JoinColumn;
- import javax.persistence.ManyToOne;
- import javax.persistence.Table;
- @Entity
- @Table(name="ORDERS")
- public class Order implements Serializable{
- private static final long serialVersionUID = 1L;
- public Order(){}
- public Order(String orderNumber){
- this.orderNumber =orderNumber;
- }
- @Id
- @GeneratedValue
- @Column(name="ID")
- private Long id;
- @Column(name="ORDER_NUMBER",length=15)
- private String orderNumber;
- @ManyToOne(fetch = FetchType.EAGER,cascade=CascadeType.ALL)
- @JoinColumn(name="CUSTOMER_ID")
- private Customer customer;
- public Long getId() {
- return id;
- }
- public void setId(Long id) {
- this.id = id;
- }
- public String getOrderNumber() {
- return orderNumber;
- }
- public void setOrderNumber(String orderNumber) {
- this.orderNumber = orderNumber;
- }
- public Customer getCustomer() {
- return customer;
- }
- public void setCustomer(Customer customer) {
- this.customer = customer;
- }
- }
[java] view plaincopy
- package com.lwp.hibernate.Model;
- import java.io.Serializable;
- 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;
- @Entity
- @Table
- public class Customer implements Serializable{
- private static final long serialVersionUID = 1L;
- public Customer(){}
- public Customer(String name,Set<Order> orders){
- this.name=name;
- this.orders = orders;
- }
- @Id
- @GeneratedValue
- @Column(name="ID")
- private Long id;
- @Column(name="NAME",length=15)
- private String name;
- @OneToMany(mappedBy="customer")
- private Set<Order> orders = new HashSet<Order>();
- public Set<Order> getOrders() {
- return orders;
- }
- public void setOrders(Set<Order> orders) {
- this.orders = orders;
- }
- public Long getId() {
- return id;
- }
- public void setId(Long id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- }
[java] view plaincopy
- import java.util.HashSet;
- import java.util.Set;
- import org.hibernate.Session;
- import org.hibernate.SessionFactory;
- import org.hibernate.cfg.AnnotationConfiguration;
- import org.hibernate.cfg.Configuration;
- import org.hibernate.tool.hbm2ddl.SchemaExport;
- import com.lwp.hibernate.Model.Customer;
- import com.lwp.hibernate.Model.Order;
- public class CustomerTest {
- public static void main(String[] args) {
- new SchemaExport(new AnnotationConfiguration().configure("/hibernate.cfg.xml")).create(true, false);
- Set<Order> orders = new HashSet<Order>();
- Order order1 = new Order("Tom_Order001");
- Order order2 = new Order("Tom_Order002");
- orders.add(order1);
- orders.add(order2);
- Customer customer = new Customer("Tom",orders);
- order1.setCustomer(customer);
- order2.setCustomer(customer);
- //save(customer);
- save(order1);
- save(order2);
- }
- public static void save(Object o){
- //Configuration cfg = new Configuration();//xx.hbm.xml形式下的new
- Configuration cfg = new AnnotationConfiguration(); //Annotation形式下的new
- SessionFactory sf = cfg.configure().buildSessionFactory();
- Session session = sf.openSession();
- session.beginTransaction();
- session.save(o);
- session.getTransaction().commit();
- session.close();
- sf.close();
- }
- }
Hibernate级联关系OneToMany之 Annotation实现
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。