日本搞逼视频_黄色一级片免费在线观看_色99久久_性明星video另类hd_欧美77_综合在线视频

國內最全IT社區平臺 聯系我們 | 收藏本站
阿里云優惠2
您當前位置:首頁 > php框架 > 框架設計 > Hibernate之一對多關聯映射

Hibernate之一對多關聯映射

來源:程序員人生   發布時間:2016-03-24 09:18:29 閱讀次數:3891次

這里以顧客、定單為例。1個顧客對應著多個定單。
數據表以下:
顧客表訂單表

在雙向1對多關聯映照中,需要在”1“的1方添加set屬性來寄存”多“的1方,在屬性上添加@OneToMany注解,mapping指向”1“的表。
同時在”多“的1方,添加”多“的對象屬性,在并在這個屬性上加上@ManyToOne注解 和@JoinColumn注解,后者的name值為”多“的1方的表中外鍵列的列名。
需要注意的是,1旦使用1對多關聯映照,數據庫中1定要添加外鍵,外鍵對應的是”1“的1方的主鍵,所以類型1定是1樣的。
實體類:

@Entity @Table(name = "customer", catalog = "etoak") public class Customer implements java.io.Serializable { private Integer id; private String name; private Setorderses = new HashSet(0); public Customer() { } public Customer(String name, Setorderses) { this.name = name; this.orderses = orderses; } @Id @GeneratedValue(strategy = IDENTITY) @Column(name = "id", unique = true, nullable = false) public Integer getId() { return this.id; } public void setId(Integer id) { this.id = id; } @Column(name = "name", length = 32) public String getName() { return this.name; } public void setName(String name) { this.name = name; } @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "customer") public SetgetOrderses() { return this.orderses; } public void setOrderses(Setorderses) { this.orderses = orderses; } }
@Entity @Table(name = "orders", catalog = "etoak") public class Orders implements java.io.Serializable { private Integer id; private Customer customer; private Integer money; public Orders() { } public Orders(Customer customer, Integer money) { this.customer = customer; this.money = money; } @Id @GeneratedValue(strategy = IDENTITY) @Column(name = "id", unique = true, nullable = false) public Integer getId() { return this.id; } public void setId(Integer id) { this.id = id; } @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "cid") public Customer getCustomer() { return this.customer; } public void setCustomer(Customer customer) { this.customer = customer; } @Column(name = "money") public Integer getMoney() { return this.money; } public void setMoney(Integer money) { this.money = money; } }

dao層:

public interface IDao{ public boolean add( T obj); public boolean delete( T obj); public boolean update (T obj); public T get(Classcls,int id); }

dao層實現類:

這里注意1下:在使用hibernate的時候必須要提交事務,不然的話履行完 沒有毛病,hibernate履行語句也打印出來了,但數據表里面就是沒有添加數據。

public class DaoImpl<T> implements IDao<T> { Session session = null; Transaction tx = null; @Override public boolean add(T obj) { try { session = SF.getSession(); tx = session.beginTransaction(); session.save(obj); tx.commit(); return true; } catch (Exception e) { e.printStackTrace(); return false; }finally{ if(session!=null)session.close(); } } public boolean delete(T obj){ try{ session = SF.getSession(); tx = session.beginTransaction(); session.delete(obj); tx.commit(); return true; }catch(Exception e){ e.printStackTrace(); return false; }finally{ if(session!=null) session.close(); } } public boolean update(T obj){ try { session = SF.getSession(); tx = session.beginTransaction(); session.update(obj); tx.commit(); return true; } catch (Exception e) { e.printStackTrace(); return false; } } @Override public T get(Class<T> cls, int id) { try { session = SF.getSession(); tx = session.beginTransaction(); T t =(T) session.get(cls,id); tx.commit(); return t; } catch (Exception e) { e.printStackTrace(); return null; }finally{ if(session!=null)session.close(); } } }

測試類:

public class Test { public static void main(String[] args) { //添加 1 /* 這里注意1下,在添加Customer也就是“1”時,必須要給orders setCustomer值,不然添加后,orders表添加的那1列外鍵值會為空。 */ Customer cus = new Customer(); cus.setName("etoak"); Orders o1 = new Orders(); o1.setMoney(100); Orders o2 = new Orders(); o2.setMoney(200); cus.getOrderses().add(o1); cus.getOrderses().add(o2); o1.setCustomer(cus);o2.setCustomer(cus); IDaodao = new DaoImpl(); dao.add(cus); //添加 多 Orders o1 = new Orders(); o1.setMoney(1001); IDaocusDao = new DaoImpl(); Customer cus = cusDao.get(Customer.class, 2); o1.setCustomer(cus); IDaodao = new DaoImpl(); dao.add(o1); //刪除 多 /*Orders o1 = new Orders(); o1.setId(2); IDaodao = new DaoImpl(); dao.delete(o1);*/ //刪除 1 /*Customer cus = new Customer(); cus.setId(2); IDaodao = new DaoImpl(); dao.delete(cus);*/ } }

session工廠類 :
這里要說明1下。 就是使用注解方式和非注解方式這個工廠類是有1點不同的。new 的configuration 不1樣。注解方式用的是AnnotationConfiguration類

public class SF { private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml"; private static final ThreadLocalthreadLocal = new ThreadLocal(); private static Configuration configuration = new AnnotationConfiguration(); private static org.hibernate.SessionFactory sessionFactory; private static String configFile = CONFIG_FILE_LOCATION; static { try { configuration.configure(configFile); sessionFactory = configuration.buildSessionFactory(); } catch (Exception e) { System.err .println("%%%% Error Creating SessionFactory %%%%"); e.printStackTrace(); } } private SF() { } public static Session getSession() throws HibernateException { Session session = (Session) threadLocal.get(); if (session == null || !session.isOpen()) { if (sessionFactory == null) { rebuildSessionFactory(); } session = (sessionFactory != null) ? sessionFactory.openSession() : null; threadLocal.set(session); } return session; } public static void rebuildSessionFactory() { try { configuration.configure(configFile); sessionFactory = configuration.buildSessionFactory(); } catch (Exception e) { System.err .println("%%%% Error Creating SessionFactory %%%%"); e.printStackTrace(); } } public static void closeSession() throws HibernateException { Session session = (Session) threadLocal.get(); threadLocal.set(null); if (session != null) { session.close(); } } public static org.hibernate.SessionFactory getSessionFactory() { return sessionFactory; } public static void setConfigFile(String configFile) { SF.configFile = configFile; sessionFactory = null; } public static Configuration getConfiguration() { return configuration; } }

hibernate.cfg.xml

<hibernate-configuration> <session-factory> <property name="dialect">org.hibernate.dialect.MySQLDialectproperty> <property name="connection.url">jdbc:mysql://localhost:3306/etoakproperty> <property name="connection.username">rootproperty> <property name="connection.password">rootproperty> <property name="connection.driver_class">com.mysql.jdbc.Driverproperty> <property name="myeclipse.connection.profile">mysqlproperty> <property name="show_sql">trueproperty> <property name="format_sql">trueproperty> <mapping class="com.etoak.entity.Student"/> <mapping class="com.etoak.entity.Classes"/> session-factory> hibernate-configuration>


生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: 午夜激情视频在线观看 | 日韩精品视频一区二区三区 | 毛片免费视频 | 天天操天天射天天添 | 中文成人在线 | 国产精品一区二区三区在线 | 午夜在线免费视频 | 99九九热 | 国产午夜视频在线观看 | 国产永久免费 | 精品亚洲一区二区三区 | 成人a毛片 | 久久久极品 | 99欧美视频 | 欧美一区二区三区 | 精品欧美一区二区精品久久久 | 国产69久久精品成人看 | 精品一区二区三区在线播放 | 成人av激情 | 国产精品高清在线观看 | 夜夜春视频 | 亚洲欧美日韩天堂 | 久久久网站 | 国产乱码精品一区二区三区五月婷 | www.国产91 | av资源免费 | 亚洲一区 在线播放 | 久久中文字幕一区二区 | 国产精品永久免费 | 久久久久久久久久久久久9999 | 久久免费精品 | 国产精品国产三级国产在线观看 | 国产午夜精品在线观看 | 91精品国产色综合久久不卡98口 | 精品一区二区av | 色综合视频在线观看 | 在线免费观看毛片 | 日本精品在线视频 | 精品视频在线免费看 | 不卡在线视频 | 国产一区二区三区四区五区tv |