[Hibernate] - many to many
Hibernate的多对多实现:
hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- Database connection settings --> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://127.0.0.1/testdb</property> <property name="connection.username">root</property> <property name="connection.password"></property> <!-- JDBC connection pool (use the built-in) --> <property name="connection.pool_size">1</property> <!-- SQL dialect --> <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property> <!-- Enable Hibernate‘s automatic session context management --> <property name="current_session_context_class">thread</property> <!-- Disable the second-level cache --> <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property> <!-- Echo all executed SQL to stdout --> <property name="show_sql">true</property> <!-- Drop and re-create the database schema on startup --> <property name="hbm2ddl.auto">update</property> <mapping resource="com/my/hbm/User.hbm.xml"/> <mapping resource="com/my/hbm/Bank.hbm.xml"/> </session-factory> </hibernate-configuration>
mapping配置:
Bank.hbm.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.my.bean.Bank" table="bank"> <id name="bankID" column="bank_id" type="long"> <generator class="native"></generator> </id> <set name="users" table="card"> <key column="bank_id"></key> <many-to-many class="com.my.bean.User" column="user_id"></many-to-many> </set> <property name="bankName" column="bank_name" type="java.lang.String" length="100" not-null="true"></property> </class> </hibernate-mapping>
User.hbm.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.my.bean.User" table="user"> <id name="userID" column="user_id" type="long"> <generator class="native"></generator> </id> <set name="banks" table="card"> <key column="user_id"></key> <many-to-many class="com.my.bean.Bank" column="bank_id"></many-to-many> </set> <property name="userName" column="user_name" type="java.lang.String" length="100" not-null="true"></property> </class> </hibernate-mapping>
POJO
package com.my.bean; import java.util.Set; public class Bank { private long bankID; private String bankName; private Set<User> users; public long getBankID() { return bankID; } public void setBankID(long bankID) { this.bankID = bankID; } public String getBankName() { return bankName; } public void setBankName(String bankName) { this.bankName = bankName; } public Set<User> getUsers() { return users; } public void setUsers(Set<User> users) { this.users = users; } }
package com.my.bean; import java.util.Set; public class User { private long userID; private String userName; private Set<Bank> banks; public long getUserID() { return userID; } public void setUserID(long userID) { this.userID = userID; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public Set<Bank> getBanks() { return banks; } public void setBanks(Set<Bank> banks) { this.banks = banks; } }
测试:
package com.my.init; import java.util.HashSet; import org.hibernate.Session; import org.hibernate.Transaction; import com.my.bean.Bank; import com.my.bean.User; import com.my.dao.util.HibernateUtil; public class Test { public static void main(String[] args) { Session session = HibernateUtil.getSessionFactory().openSession(); Transaction tx = session.beginTransaction(); try { // create user User userRobin = new User(); userRobin.setUserName("Robin"); userRobin.setBanks(new HashSet<Bank>()); User userBen = new User(); userBen.setUserName("Ben"); userBen.setBanks(new HashSet<Bank>()); // create bank Bank bankCBC = new Bank(); bankCBC.setBankName("CBC"); bankCBC.setUsers(new HashSet<User>()); Bank bankBBC = new Bank(); bankBBC.setBankName("BBC"); bankBBC.setUsers(new HashSet<User>()); // add relationship userRobin.getBanks().add(bankCBC); userRobin.getBanks().add(bankBBC); userBen.getBanks().add(bankCBC); userBen.getBanks().add(bankBBC); session.save(userRobin); session.save(userBen); session.save(bankCBC); session.save(bankBBC); tx.commit(); } catch (Exception e) { tx.rollback(); e.printStackTrace(); } session.close(); } }
上面测试例子会自动生成一张表:card,这张是bank和user表的映射表。里头是bank_id和user_id两个组合字段。
如果想在这张映射表中加入额外的字段,那么hibernate似乎无法做到。因为这需要把多对多写成两个一对多的关系。事实上这样加额外字段到映射表,好像也是违反了DBMS的设计原则。
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。