hibernate多对多 本帖最后由 shmily2038 于 2010-01-08 00:46:27 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 使用Hibernate原生sql查询吧。 这里有一个图书的例子:public class ManyToManyDAO extends BaseHibernateDAO { // 插入books表 public void saveBook() { Session session = this.getSession(); Transaction tx = session.beginTransaction(); try { Books book = new Books(); book.setBname("php入门"); book.setPrice(new Double(100)); session.save(book); tx.commit(); } catch (HibernateException e) { tx.rollback(); e.printStackTrace(); } finally { session.close(); } } // 插入bookOrders表 public void saveOrder() { Session session = this.getSession(); Transaction tx = session.beginTransaction(); try { Bookorder order = new Bookorder(); order.setOnumber("A002"); session.save(order); tx.commit(); } catch (HibernateException e) { tx.rollback(); e.printStackTrace(); } finally { session.close(); } } // 插入join表 public void saveJoin() { Session session = this.getSession(); Transaction tx = session.beginTransaction(); try { // 查询两个主表记录 Books book = (Books) session.load(Books.class, new Integer(25)); Bookorder order = (Bookorder) session.load(Bookorder.class, new Integer(12)); // 设置关系 book.getBookorders().add(order); order.getBookses().add(book); tx.commit(); } catch (HibernateException e) { tx.rollback(); e.printStackTrace(); } finally { session.close(); } } public Bookorder findOrder() { Session session = this.getSession(); try{ return (Bookorder) session.get(Bookorder.class, new Integer(12)); } finally { session.close(); } } public void print(Bookorder ord) { if (ord != null) { String orderNumber = ord.getOnumber(); Iterator it = ord.getBookses().iterator(); while (it.hasNext()) { Books book = (Books) it.next(); System.out.println("订单编号:" + orderNumber + " 订购商品: " + book.getBname() + " 价格: " + book.getPrice()); } } } public void deleteJoinAll() { Session session = this.getSession(); Transaction tx = session.beginTransaction(); try { // 查询两个主表记录 Bookorder order = (Bookorder) session.load(Bookorder.class, new Integer(12)); // 解除关系 Iterator it = order.getBookses().iterator(); while(it.hasNext()) { Books b = (Books) it.next(); b.getBookorders().remove(order); } order.getBookses().removeAll(order.getBookses()); tx.commit(); } catch (HibernateException e) { tx.rollback(); e.printStackTrace(); } finally { session.close(); } } public void deleteJoin() { Session session = this.getSession(); Transaction tx = session.beginTransaction(); try { // 查询两个主表记录 Books book = (Books) session.load(Books.class, new Integer(24)); Bookorder order = (Bookorder) session.load(Bookorder.class, new Integer(12)); // 解除关系 book.getBookorders().remove(order); order.getBookses().remove(book); tx.commit(); } catch (HibernateException e) { tx.rollback(); e.printStackTrace(); } finally { session.close(); } } public void updateJoin() { Session session = this.getSession(); Transaction tx = session.beginTransaction(); try { // 查询原有两个主表记录 Books book = (Books) session.load(Books.class, new Integer(22)); Bookorder order = (Bookorder) session.load(Bookorder.class, new Integer(12)); // 解除关系 book.getBookorders().remove(order); order.getBookses().remove(book); //查询新的关联表 Books newBook = (Books) session.load(Books.class, new Integer(23)); //重新建立关系 newBook.getBookorders().add(order); order.getBookses().add(newBook); tx.commit(); } catch (HibernateException e) { tx.rollback(); e.printStackTrace(); } finally { session.close(); } } public void deleteOrder() { Session session = this.getSession(); Transaction tx = session.beginTransaction(); try { //查询订单表 Bookorder order = (Bookorder) session.load(Bookorder.class, new Integer(13)); //删除 session.delete(order); tx.commit(); } catch (HibernateException e) { tx.rollback(); e.printStackTrace(); } finally { session.close(); } } public static void main(String[] args) { ManyToManyDAO dao = new ManyToManyDAO(); // dao.saveBook(); // dao.saveOrder(); //dao.saveJoin(); //dao.print(dao.findOrder()); //dao.deleteJoin(); //dao.deleteJoinAll(); //dao.updateJoin(); dao.deleteOrder(); } 不是你这个意思吧,我建立一个User表,建立一个Firend表,建立一个中间表Temp。多对多映射文件应该是,User文件中是User - Firend,Firend映射文件中是Firend - User。如果你要建立中间表的Bean,那么应该这么写也没问题。User - Temp, Firend - Temp。还有,下次尽量把问题写清楚,写规范,英文都懒得打一张,你那个什么鬼XX set XX谁能看得懂. 正好有个 你看看<hibernate-mapping> <class name="com.bjsxt.hibernate.User" table="t_user"> <id name="id"> <generator class="native"/> </id> <property name="name"/> <set name="roles" table="t_user_role"> <key column="userid"/> <many-to-many class="com.bjsxt.hibernate.Role" column="roleid"/> </set> </class></hibernate-mapping><hibernate-mapping> <class name="com.bjsxt.hibernate.Role" table="t_role"> <id name="id"> <generator class="native"/> </id> <property name="name"/> <set name="users" table="t_user_role" order-by="userid"> <key column="roleid"/> <many-to-many class="com.bjsxt.hibernate.User" column="userid"/> </set> </class></hibernate-mapping> 关于权限的问题? struts2+spring整合登录问题 一对多,懒加载,能使用二级缓存吗 初学者的问题(谁能给一个struts+hibernate最简单的例子) resin服务器如何绑定域名啊,急... Tomcat 5.0.27+j2re1.4.2_02的问题。 有谁用过Eclipse3.0+tomcatPluginV3.zip+tomcat5的环境配置?怎么配置? 请问各朋友怎样写这监听器 编写实体Bean时出的错误和一些迷惑,请高手指点迷津。 ★★JBuilder8+JBoss3+Oracle9 中文问题★★ 求助算法 求助算法
public class ManyToManyDAO extends BaseHibernateDAO {
// 插入books表
public void saveBook() {
Session session = this.getSession();
Transaction tx = session.beginTransaction();
try {
Books book = new Books();
book.setBname("php入门");
book.setPrice(new Double(100));
session.save(book);
tx.commit();
} catch (HibernateException e) {
tx.rollback();
e.printStackTrace();
} finally {
session.close();
}
} // 插入bookOrders表
public void saveOrder() {
Session session = this.getSession();
Transaction tx = session.beginTransaction();
try {
Bookorder order = new Bookorder();
order.setOnumber("A002");
session.save(order);
tx.commit();
} catch (HibernateException e) {
tx.rollback();
e.printStackTrace();
} finally {
session.close();
}
} // 插入join表
public void saveJoin() {
Session session = this.getSession();
Transaction tx = session.beginTransaction();
try {
// 查询两个主表记录
Books book = (Books) session.load(Books.class, new Integer(25));
Bookorder order = (Bookorder) session.load(Bookorder.class,
new Integer(12));
// 设置关系
book.getBookorders().add(order);
order.getBookses().add(book); tx.commit();
} catch (HibernateException e) {
tx.rollback();
e.printStackTrace();
} finally {
session.close();
}
} public Bookorder findOrder() {
Session session = this.getSession();
try{
return (Bookorder) session.get(Bookorder.class, new Integer(12));
} finally {
session.close();
}
} public void print(Bookorder ord) {
if (ord != null) {
String orderNumber = ord.getOnumber();
Iterator it = ord.getBookses().iterator();
while (it.hasNext()) {
Books book = (Books) it.next();
System.out.println("订单编号:" + orderNumber + " 订购商品: "
+ book.getBname() + " 价格: " + book.getPrice());
}
}
}
public void deleteJoinAll() {
Session session = this.getSession();
Transaction tx = session.beginTransaction();
try {
// 查询两个主表记录
Bookorder order = (Bookorder) session.load(Bookorder.class,
new Integer(12));
// 解除关系
Iterator it = order.getBookses().iterator();
while(it.hasNext()) {
Books b = (Books) it.next();
b.getBookorders().remove(order);
}
order.getBookses().removeAll(order.getBookses());
tx.commit();
} catch (HibernateException e) {
tx.rollback();
e.printStackTrace();
} finally {
session.close();
}
}
public void deleteJoin() {
Session session = this.getSession();
Transaction tx = session.beginTransaction();
try {
// 查询两个主表记录
Books book = (Books) session.load(Books.class, new Integer(24));
Bookorder order = (Bookorder) session.load(Bookorder.class,
new Integer(12));
// 解除关系
book.getBookorders().remove(order);
order.getBookses().remove(book); tx.commit();
} catch (HibernateException e) {
tx.rollback();
e.printStackTrace();
} finally {
session.close();
}
}
public void updateJoin() {
Session session = this.getSession();
Transaction tx = session.beginTransaction();
try {
// 查询原有两个主表记录
Books book = (Books) session.load(Books.class, new Integer(22));
Bookorder order = (Bookorder) session.load(Bookorder.class,
new Integer(12));
// 解除关系
book.getBookorders().remove(order);
order.getBookses().remove(book);
//查询新的关联表
Books newBook = (Books) session.load(Books.class, new Integer(23));
//重新建立关系
newBook.getBookorders().add(order);
order.getBookses().add(newBook);
tx.commit();
} catch (HibernateException e) {
tx.rollback();
e.printStackTrace();
} finally {
session.close();
}
} public void deleteOrder() {
Session session = this.getSession();
Transaction tx = session.beginTransaction();
try {
//查询订单表
Bookorder order = (Bookorder) session.load(Bookorder.class, new Integer(13));
//删除
session.delete(order);
tx.commit();
} catch (HibernateException e) {
tx.rollback();
e.printStackTrace();
} finally {
session.close();
}
}
public static void main(String[] args) {
ManyToManyDAO dao = new ManyToManyDAO();
// dao.saveBook();
// dao.saveOrder();
//dao.saveJoin();
//dao.print(dao.findOrder());
//dao.deleteJoin();
//dao.deleteJoinAll();
//dao.updateJoin();
dao.deleteOrder();
}
<hibernate-mapping>
<class name="com.bjsxt.hibernate.User" table="t_user">
<id name="id">
<generator class="native"/>
</id>
<property name="name"/>
<set name="roles" table="t_user_role">
<key column="userid"/>
<many-to-many class="com.bjsxt.hibernate.Role" column="roleid"/>
</set>
</class>
</hibernate-mapping>
<hibernate-mapping>
<class name="com.bjsxt.hibernate.Role" table="t_role">
<id name="id">
<generator class="native"/>
</id>
<property name="name"/>
<set name="users" table="t_user_role" order-by="userid">
<key column="roleid"/>
<many-to-many class="com.bjsxt.hibernate.User" column="userid"/>
</set>
</class>
</hibernate-mapping>