One of Hibernate's multi-Association ing
Here, the customer and order are used as examples. One customer corresponds to multiple orders.
The data table is as follows:
In bidirectional one-to-Multiple Association ing, you must add the set attribute to the "one" side to store the "multiple" side, and add the @ onetoworkflow annotation to the attribute, mapping points to the "1" table.
At the same time, add the "multiple" Object attribute on the "multiple" side, add the @ ManyToOne annotation and @ JoinColumn Annotation on this attribute, the name value of the latter is the column name of the foreign key column in the table of the "multiple" side.
Note that once one-to-Multiple Association ing is used, a foreign key must be added to the database. The foreign key corresponds to the primary key of one party, so the types must be the same.
Entity class:
@Entity@Table(name = customer, catalog = etoak)public class Customer implements java.io.Serializable { private Integer id; private String name; private Set
orderses = new HashSet
(0); public Customer() { } public Customer(String name, Set
orderses) { 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 Set
getOrderses() { return this.orderses; } public void setOrderses(Set
orderses) { 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 layer:
public interface IDao
{ public boolean add( T obj); public boolean delete( T obj); public boolean update (T obj); public T get(Class
cls,int id);}
Dao implementation class:
Note: When Using hibernate, you must submit the transaction. Otherwise, there will be no errors after the execution, and the hibernate execution statement will be printed out, but no data is added to the data table.
public class DaoImpl
implements IDao
{ 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
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(); } }}
Test class:
Public class Test {public static void main (String [] args) {// Add one/* Note that when a Customer is added, you must set the value of orders setCustomer. Otherwise, the foreign key value of the column added to the orders table will be blank. */Customer cus = new Customer (); cus. setName (etoak); Orders o1 = new Orders (); o1.setMoney (100); Orders o2 = new Orders (); o2.setMoney (200); cuz. getOrderses (). add (o1); cus. getOrderses (). add (o2); o1.setCustomer (cuz); o2.setCustomer (cuz); IDao
Dao = new DaoImpl
(); Dao. add (cus); // add Multiple Orders o1 = new Orders (); o1.setMoney (1001); IDao
CusDao = new DaoImpl
(); Customer cus = cusDao. get (Customer. class, 2); o1.setCustomer (cus); IDao
Dao = new DaoImpl
(); Dao. add (o1); // Delete multiple/* Orders o1 = new Orders (); o1.setId (2); IDao
Dao = new DaoImpl
(); Dao. delete (o1); * // delete A/* Customer cus = new Customer (); cus. setId (2); IDao
Dao = new DaoImpl
(); Dao. delete (cus );*/}}
Session factory class:
It should be noted here. That is, using annotation and non-annotation methods is a little different from the factory class. The configuration of new is different. The annotation method uses the AnnotationConfiguration class.
public class SF { private static String CONFIG_FILE_LOCATION = /hibernate.cfg.xml; private static final ThreadLocal
threadLocal = 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
org.hibernate.dialect.MySQLDialect
jdbc:mysql://localhost:3306/etoak
root
root
com.mysql.jdbc.Driver
mysql
true
true