JPA双向多对多关联关系@ManyToMany

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_36722039/article/details/81057897
package com.jpa.helloworld;

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.ManyToMany;
import javax.persistence.Table;

@Table(name="CATEGORYS")
@Entity
public class Category {

	private Integer cID;

	private String cName;

	private Set<Item> items = new HashSet<Item>();

	@Id
	@Column(name="C_ID")
	@GeneratedValue
	public Integer getcID() {
		return cID;
	}

	public void setcID(Integer cID) {
		this.cID = cID;
	}

	@Column(name="C_NAME")
	public String getcName() {
		return cName;
	}

	public void setcName(String cName) {
		this.cName = cName;
	}
	
	//添加了mappedBy属性则不能使用@JoinTable注解
	@ManyToMany(mappedBy="categorys")
	public Set<Item> getItems() {
		return items;
	}

	public void setItems(Set<Item> items) {
		this.items = items;
	}

}

package com.jpa.helloworld;

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.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;

@Table(name="ITEMS")
@Entity
public class Item {

	private Integer iId;

	private String iName;

	private Set<Category> categorys = new HashSet<Category>();

	@Id
	@GeneratedValue
	@Column(name="I_ID")
	public Integer getiId() {
		return iId;
	}

	public void setiId(Integer iId) {
		this.iId = iId;
	}

	@Column(name="I_NAME")
	public String getiName() {
		return iName;
	}

	public void setiName(String iName) {
		this.iName = iName;
	}

	//使用@JoinTable注解添加中间表
	//其中name属性设置中间表的表名
	//joinCloums属性在中间表中添加的列
	//JoinColumns属性:
	//				@joinColumn属性设置中间表中的列名
	//						referencedColumnName属性指向被映射表的主键(可以没有该属性)
	//@inverseJoinColumns另外一张表在中间表中的列
	@JoinTable(
			name="ITEMS_CATEGORYS",
//			joinColumns = {@JoinColumn(name="ITEM_ID",referencedColumnName="I_ID")},
			joinColumns = {@JoinColumn(name="ITEM_ID")},
//			inverseJoinColumns= {@JoinColumn(name="CATEGORY_ID", referencedColumnName="C_ID")})
			inverseJoinColumns= {@JoinColumn(name="CATEGORY_ID")})
	@ManyToMany
	public Set<Category> getCategorys() {
		return categorys;
	}

	public void setCategorys(Set<Category> categorys) {
		this.categorys = categorys;
	}

}

展开阅读全文

hibernate 3表以上 多对多 双向关联关系

11-16

hibernate两张表之间的多对多双向联系,我已经会使用,但是,现在是3张表之间的多对多,不知道如何配置。rn问题来源:Martin Fowler提出过:用户——角色——权限 的模型,所以现在做了一个系统:一张用户表、一张角色表、一张权限表,并有如下两张关联表:用户-角色关联表、角色-权限关联表。希望查找用户的时候,自动把他的所有角色、权限查询出来。其实,说白了,也就是把两个多对多组合在一起,怎么做!rnrnuser.javarn private List role = new ArrayList();rnuser.hbm.xmlrn rn rn rn rn rnrnrole.javarn private List user = new ArrayList();rn private List permission = new ArrayList();rnrole.hbm.xmlrn rn rn rn rn rnrn rn rn rn rn rnrnpermission.javarn private List role = new ArrayList();rnpermission.hbm.xmlrn rn rn rn rn rnrn这样配置后,当我查询user的时候,按理应该把role和permission都查询出来,可是现在报如下错误:rnERROR org.hibernate.util.JDBCExceptionReporter - [Microsoft][SQLServer 2000 Driver for JDBC]Value can not be converted to requested type.rnERROR common.dao.BaseDAO - excuting from User as model where 1=1 and model.name like ? and model.password like ? with value[] failedrnorg.springframework.jdbc.UncategorizedSQLException: Hibernate operation: could not initialize a collection: [cn.ncsimvpq.user.domain.User.role#ABCDEFGHIJKLMZYXWVUTSRQPON0123456782]; uncategorized SQLException for SQL [select role0_.user_id as user2_1_, role0_.role_id as role1_1_, role0_.id as id1_, role1_.id as id7_0_, role1_.name as name7_0_, role1_.description as descript3_7_0_, role1_.create_user as create4_7_0_, role1_.create_time as create5_7_0_, role1_.update_user as update6_7_0_, role1_.update_time as update7_7_0_, role1_.delete_flag as delete8_7_0_ from emcl_user_role role0_ left outer join emcl.dbo.emcl_role role1_ on role0_.role_id=role1_.id where role0_.user_id=?]; SQL state [HY000]; error code [0]; [Microsoft][SQLServer 2000 Driver for JDBC]Value can not be converted to requested type.; nested exception is java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Value can not be converted to requested type.rnrn不知道哪里有问题,亦或是我的思路不对,求大虾指导 论坛

没有更多推荐了,返回首页