有3个表
/**
* 角色基本信息
*
*/
@SuppressWarnings("serial")
@Entity
@Table(name="Role", catalog="hiic",schema="dbo")
public class Role implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
//编号,名称
@Id
private String id;
private String name;
private String description;
//用户列表 List<User>
@ManyToMany(cascade = CascadeType.REFRESH,mappedBy = "roleSet", fetch = FetchType.LAZY)
private Set<User> userSet =new HashSet<User>();
//操作权限列表 List<Operation>
@ManyToMany(cascade = CascadeType.REFRESH)
@JoinTable( catalog="ManyToMany", name="RoleOperation",joinColumns={@JoinColumn(name="id")},inverseJoinColumns={@JoinColumn(name="RoleOperationPK.operation")} )
private Set<Operation> operationSet =new HashSet<Operation>();
public Set<Operation> getOperationSet() {
return operationSet;
}
public void setOperationSet(Set<Operation> operationSet) {
this.operationSet = operationSet;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Set<User> getUserSet() {
return userSet;
}
public void setUserSet(Set<User> userSet) {
this.userSet = userSet;
}
操作权限@SuppressWarnings("serial")
@Entity
//@Table(name="Operation", catalog="hiic",schema="dbo")
public class Operation implements java.io.Serializable {
//编号
@Id
@GeneratedValue
private String id;
//名称
private String name;
//描述
private String description;
//顺序
@Enumerated(EnumType.ORDINAL)
private Integer sequenceq;
//功能模块
private String module;
//角色列表
@ManyToMany(cascade = CascadeType.REFRESH, mappedBy = "operationSet", fetch = FetchType.LAZY)
// @ManyToMany(mappedBy = "operationSet")
private Set<Role> roleSet= new HashSet<Role>();
public Set<Role> getRoleSet() {
return roleSet;
}
public void setRoleSet(Set<Role> roleSet) {
this.roleSet = roleSet;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Integer getSequenceq() {
return sequenceq;
}
public void setSequenceq(Integer sequenceq) {
this.sequenceq = sequenceq;
}
public String getModule() {
return module;
}
public void setModule(String module) {
this.module = module;
}
}这两个表是多对多的,下面是中间表
//角色操作
@SuppressWarnings("serial")
@Entity
//@Table(name="RoleOperation", catalog="hiic",schema="dbo")
public class RoleOperation implements Serializable{
/**
*
*/
private RoleOperationPK id;
public RoleOperation(){
}
public RoleOperation(RoleOperationPK rpk){
this.id=rpk;
}
@EmbeddedId
public RoleOperationPK getId() {
return id;
}
public void setId(RoleOperationPK id) {
this.id = id;
}
}
主键是联合主键,有另外一个表
@SuppressWarnings("serial")
@Embeddable
public class RoleOperationPK implements Serializable{
/**
*
*/
public RoleOperationPK(){
}
public RoleOperationPK(String role,String operation){
this.role=role;
this.operation=operation;
}
//角色
private String role;
//操作权限
private String operation;
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
public String getOperation() {
return operation;
}
public void setOperation(String operation) {
this.operation = operation;
}
@Override
public int hashCode(){
final int prime = 31;
int result = 1;
result = prime * result + ((role == null) ? 0 : role.hashCode());
result = prime * result + ((operation == null) ? 0 : operation.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final RoleOperationPK other = (RoleOperationPK) obj;
if (role == null) {
if (other.role != null)
return false;
} else if (!role.equals(other.role))
return false;
if (operation == null) {
if (other.operation != null)
return false;
} else if (!operation.equals(other.operation))
return false;
return true;
}
}然后在做更新操作的时候报错
//操作权限列表 List<Operation>
@ManyToMany(cascade = CascadeType.REFRESH)
@JoinTable( catalog="ManyToMany", name="RoleOperation",joinColumns={@JoinColumn(name="id")},inverseJoinColumns={@JoinColumn(name="RoleOperationPK.operation")} )
private Set<Operation> operationSet =new HashSet<Operation>();我想这个应该是出错的多对多映射public <T> boolean update(T entity){
EntityManager em = getEntityManagerFactory().createEntityManager();
em.getTransaction().begin();
try{
em.merge(entity);
}catch(Exception e){
e.printStackTrace();
em.getTransaction().rollback();
return false;
}
em.getTransaction().commit();
closeEntitymanager(em);
return true;
}这个是执行更新的代码....
哪位能帮忙解决下这个是报的错误
javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not initialize a collection: [com.tian.ebiz.authorization.model.Role.operationSet#fffff]
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1214)
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1147)
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1153)
at org.hibernate.ejb.AbstractEntityManagerImpl.merge(AbstractEntityManagerImpl.java:695)
at com.tian.ebiz.persistent.PersistentManager.update(PersistentManager.java:101)
at com.tian.ebiz.persistent.PersistentManager.main(PersistentManager.java:140)
Caused by: org.hibernate.exception.SQLGrammarException: could not initialize a collection: [com.tian.ebiz.authorization.model.Role.operationSet#fffff]
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:92)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.loader.Loader.loadCollection(Loader.java:2173)
at org.hibernate.loader.collection.CollectionLoader.initialize(CollectionLoader.java:62)
at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:627)
at org.hibernate.event.def.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:83)
at org.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:1862)
at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:369)
at org.hibernate.collection.PersistentSet.clear(PersistentSet.java:322)
at org.hibernate.type.CollectionType.replaceElements(CollectionType.java:502)
at org.hibernate.type.CollectionType.replace(CollectionType.java:575)
at org.hibernate.type.TypeHelper.replace(TypeHelper.java:178)
at org.hibernate.event.def.DefaultMergeEventListener.copyValues(DefaultMergeEventListener.java:563)
at org.hibernate.event.def.DefaultMergeEventListener.entityIsDetached(DefaultMergeEventListener.java:491)
at org.hibernate.event.def.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:255)
at org.hibernate.event.def.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:84)
at org.hibernate.impl.SessionImpl.fireMerge(SessionImpl.java:866)
at org.hibernate.impl.SessionImpl.merge(SessionImpl.java:850)
at org.hibernate.impl.SessionImpl.merge(SessionImpl.java:854)
at org.hibernate.ejb.AbstractEntityManagerImpl.merge(AbstractEntityManagerImpl.java:686)
... 2 more
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: 对象名 'ManyToMany.RoleOperation' 无效。
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:197)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(SQLServerStatement.java:1493)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(SQLServerPreparedStatement.java:390)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PrepStmtExecCmd.doExecute(SQLServerPreparedStatement.java:340)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:4575)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1400)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:179)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:154)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeQuery(SQLServerPreparedStatement.java:283)
at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:208)
at org.hibernate.loader.Loader.getResultSet(Loader.java:1953)
at org.hibernate.loader.Loader.doQuery(Loader.java:802)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:274)
at org.hibernate.loader.Loader.loadCollection(Loader.java:2166)
... 19 more
update:false
/**
* 角色基本信息
*
*/
@SuppressWarnings("serial")
@Entity
@Table(name="Role", catalog="hiic",schema="dbo")
public class Role implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
//编号,名称
@Id
private String id;
private String name;
private String description;
//用户列表 List<User>
@ManyToMany(cascade = CascadeType.REFRESH,mappedBy = "roleSet", fetch = FetchType.LAZY)
private Set<User> userSet =new HashSet<User>();
//操作权限列表 List<Operation>
@ManyToMany(cascade = CascadeType.REFRESH)
@JoinTable( catalog="ManyToMany", name="RoleOperation",joinColumns={@JoinColumn(name="id")},inverseJoinColumns={@JoinColumn(name="RoleOperationPK.operation")} )
private Set<Operation> operationSet =new HashSet<Operation>();
public Set<Operation> getOperationSet() {
return operationSet;
}
public void setOperationSet(Set<Operation> operationSet) {
this.operationSet = operationSet;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Set<User> getUserSet() {
return userSet;
}
public void setUserSet(Set<User> userSet) {
this.userSet = userSet;
}
操作权限@SuppressWarnings("serial")
@Entity
//@Table(name="Operation", catalog="hiic",schema="dbo")
public class Operation implements java.io.Serializable {
//编号
@Id
@GeneratedValue
private String id;
//名称
private String name;
//描述
private String description;
//顺序
@Enumerated(EnumType.ORDINAL)
private Integer sequenceq;
//功能模块
private String module;
//角色列表
@ManyToMany(cascade = CascadeType.REFRESH, mappedBy = "operationSet", fetch = FetchType.LAZY)
// @ManyToMany(mappedBy = "operationSet")
private Set<Role> roleSet= new HashSet<Role>();
public Set<Role> getRoleSet() {
return roleSet;
}
public void setRoleSet(Set<Role> roleSet) {
this.roleSet = roleSet;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Integer getSequenceq() {
return sequenceq;
}
public void setSequenceq(Integer sequenceq) {
this.sequenceq = sequenceq;
}
public String getModule() {
return module;
}
public void setModule(String module) {
this.module = module;
}
}这两个表是多对多的,下面是中间表
//角色操作
@SuppressWarnings("serial")
@Entity
//@Table(name="RoleOperation", catalog="hiic",schema="dbo")
public class RoleOperation implements Serializable{
/**
*
*/
private RoleOperationPK id;
public RoleOperation(){
}
public RoleOperation(RoleOperationPK rpk){
this.id=rpk;
}
@EmbeddedId
public RoleOperationPK getId() {
return id;
}
public void setId(RoleOperationPK id) {
this.id = id;
}
}
主键是联合主键,有另外一个表
@SuppressWarnings("serial")
@Embeddable
public class RoleOperationPK implements Serializable{
/**
*
*/
public RoleOperationPK(){
}
public RoleOperationPK(String role,String operation){
this.role=role;
this.operation=operation;
}
//角色
private String role;
//操作权限
private String operation;
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
public String getOperation() {
return operation;
}
public void setOperation(String operation) {
this.operation = operation;
}
@Override
public int hashCode(){
final int prime = 31;
int result = 1;
result = prime * result + ((role == null) ? 0 : role.hashCode());
result = prime * result + ((operation == null) ? 0 : operation.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final RoleOperationPK other = (RoleOperationPK) obj;
if (role == null) {
if (other.role != null)
return false;
} else if (!role.equals(other.role))
return false;
if (operation == null) {
if (other.operation != null)
return false;
} else if (!operation.equals(other.operation))
return false;
return true;
}
}然后在做更新操作的时候报错
//操作权限列表 List<Operation>
@ManyToMany(cascade = CascadeType.REFRESH)
@JoinTable( catalog="ManyToMany", name="RoleOperation",joinColumns={@JoinColumn(name="id")},inverseJoinColumns={@JoinColumn(name="RoleOperationPK.operation")} )
private Set<Operation> operationSet =new HashSet<Operation>();我想这个应该是出错的多对多映射public <T> boolean update(T entity){
EntityManager em = getEntityManagerFactory().createEntityManager();
em.getTransaction().begin();
try{
em.merge(entity);
}catch(Exception e){
e.printStackTrace();
em.getTransaction().rollback();
return false;
}
em.getTransaction().commit();
closeEntitymanager(em);
return true;
}这个是执行更新的代码....
哪位能帮忙解决下这个是报的错误
javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not initialize a collection: [com.tian.ebiz.authorization.model.Role.operationSet#fffff]
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1214)
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1147)
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1153)
at org.hibernate.ejb.AbstractEntityManagerImpl.merge(AbstractEntityManagerImpl.java:695)
at com.tian.ebiz.persistent.PersistentManager.update(PersistentManager.java:101)
at com.tian.ebiz.persistent.PersistentManager.main(PersistentManager.java:140)
Caused by: org.hibernate.exception.SQLGrammarException: could not initialize a collection: [com.tian.ebiz.authorization.model.Role.operationSet#fffff]
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:92)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.loader.Loader.loadCollection(Loader.java:2173)
at org.hibernate.loader.collection.CollectionLoader.initialize(CollectionLoader.java:62)
at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:627)
at org.hibernate.event.def.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:83)
at org.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:1862)
at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:369)
at org.hibernate.collection.PersistentSet.clear(PersistentSet.java:322)
at org.hibernate.type.CollectionType.replaceElements(CollectionType.java:502)
at org.hibernate.type.CollectionType.replace(CollectionType.java:575)
at org.hibernate.type.TypeHelper.replace(TypeHelper.java:178)
at org.hibernate.event.def.DefaultMergeEventListener.copyValues(DefaultMergeEventListener.java:563)
at org.hibernate.event.def.DefaultMergeEventListener.entityIsDetached(DefaultMergeEventListener.java:491)
at org.hibernate.event.def.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:255)
at org.hibernate.event.def.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:84)
at org.hibernate.impl.SessionImpl.fireMerge(SessionImpl.java:866)
at org.hibernate.impl.SessionImpl.merge(SessionImpl.java:850)
at org.hibernate.impl.SessionImpl.merge(SessionImpl.java:854)
at org.hibernate.ejb.AbstractEntityManagerImpl.merge(AbstractEntityManagerImpl.java:686)
... 2 more
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: 对象名 'ManyToMany.RoleOperation' 无效。
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:197)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(SQLServerStatement.java:1493)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(SQLServerPreparedStatement.java:390)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PrepStmtExecCmd.doExecute(SQLServerPreparedStatement.java:340)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:4575)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1400)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:179)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:154)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeQuery(SQLServerPreparedStatement.java:283)
at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:208)
at org.hibernate.loader.Loader.getResultSet(Loader.java:1953)
at org.hibernate.loader.Loader.doQuery(Loader.java:802)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:274)
at org.hibernate.loader.Loader.loadCollection(Loader.java:2166)
... 19 more
update:false
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货