Hibernate映射mappedBy和@JoinColumn

时间:2015-09-19 11:40:32

标签: java mysql spring hibernate hibernate-annotations

我使用spring mvc和hibernate with annotation, 我在我的模块实体类

中有以下映射
@OneToMany(cascade=CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name="mp_mod_id")
private Set<ModulePermissionMpg> modulePermissionMpg;
//getter and setter

我的ModulePermissionsMpg实体类有

@ManyToOne(fetch = FetchType.LAZY)
private ModuleMst moduleMst; 
当我插入具有四个权限日志的模块时,使用此配置

如下

Hibernate: insert into a_modules (mod_created_date, mod_icon, mod_modified_date, mod_name, mod_status) values (?, ?, ?, ?, ?)
Hibernate: insert into a_module_permissions (moduleMst_mod_id, mp_created_by, mp_created_date, mp_mod_id, mp_modified_date, mp_perm_id, mp_status) values (?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into a_module_permissions (moduleMst_mod_id, mp_created_by, mp_created_date, mp_mod_id, mp_modified_date, mp_perm_id, mp_status) values (?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into a_module_permissions (moduleMst_mod_id, mp_created_by, mp_created_date, mp_mod_id, mp_modified_date, mp_perm_id, mp_status) values (?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into a_module_permissions (moduleMst_mod_id, mp_created_by, mp_created_date, mp_mod_id, mp_modified_date, mp_perm_id, mp_status) values (?, ?, ?, ?, ?, ?, ?)
Hibernate: update a_module_permissions set mp_mod_id=? where mp_id=?
Hibernate: update a_module_permissions set mp_mod_id=? where mp_id=?
Hibernate: update a_module_permissions set mp_mod_id=? where mp_id=?
Hibernate: update a_module_permissions set mp_mod_id=? where mp_id=?

这里的第一个问题是,“moduleMst_mod_id”这个额外的列被插入NULL。已经有一个名为mp_mod_id的列,其中包含mod_id。

第二个问题是当我尝试更新此表时删除2权限并在此模块中添加了1个新权限日志如下

insert into a_module_permissions (moduleMst_mod_id, mp_created_by, mp_created_date, mp_mod_id, mp_modified_date, mp_perm_id, mp_status) values (?, ?, ?, ?, ?, ?, ?)
Hibernate: update a_modules set mod_created_date=?, mod_icon=?, mod_modified_date=?, mod_name=?, mod_status=? where mod_id=?
Hibernate: update a_module_permissions set moduleMst_mod_id=?, mp_created_by=?, mp_created_date=?, mp_mod_id=?, mp_modified_date=?, mp_perm_id=?, mp_status=? where mp_id=?
Hibernate: update a_module_permissions set moduleMst_mod_id=?, mp_created_by=?, mp_created_date=?, mp_mod_id=?, mp_modified_date=?, mp_perm_id=?, mp_status=? where mp_id=?
Hibernate: update a_module_permissions set mp_mod_id=null where mp_mod_id=?

org.hibernate.exception.ConstraintViolationException: could not execute statement
    at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:129)
    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:126)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:112)
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:211)
    at org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.java:62)
    at org.hibernate.persister.collection.AbstractCollectionPersister.remove(AbstractCollectionPersister.java:1211)
    at org.hibernate.action.internal.CollectionRemoveAction.execute(CollectionRemoveAction.java:113)
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:463)
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:349)
    at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:350)
    at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:56)
    at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1222)
    at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:425)
    at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101)
    at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:177)
    at com.imobdev.dao.ImderpDaoImpl.update(ImderpDaoImpl.java:136)
    at com.imobdev.service.ImderpServiceImpl.update(ImderpServiceImpl.java:79)
    at com.imobdev.controller.ErpController.updateModule(ErpController.java:384)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:777)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:706)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:943)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:868)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at com.imobdev.controller.SessionFilter.doFilter(SessionFilter.java:43)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:423)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1079)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:625)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:318)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)
Caused by: com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException: Column 'mp_mod_id' cannot be null
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:931)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2941)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1623)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1715)
    at com.mysql.jdbc.Connection.execSQL(Connection.java:3249)
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1268)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1541)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1455)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1440)
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:208)
    ... 54 more

当我尝试在我的模块实体类

中使用以下配置时
@OneToMany(mappedBy="mp_mod_id", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
 private Set<ModulePermissionMpg> modulePermissionMpg;
当我插入具有四个权限日志的模块时,使用此配置

如下

Hibernate: insert into a_modules (mod_created_date, mod_icon, mod_modified_date, mod_name, mod_status) values (?, ?, ?, ?, ?)
Hibernate: insert into a_module_permissions (moduleMst_mod_id, mp_created_by, mp_created_date, mp_mod_id, mp_modified_date, mp_perm_id, mp_status) values (?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into a_module_permissions (moduleMst_mod_id, mp_created_by, mp_created_date, mp_mod_id, mp_modified_date, mp_perm_id, mp_status) values (?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into a_module_permissions (moduleMst_mod_id, mp_created_by, mp_created_date, mp_mod_id, mp_modified_date, mp_perm_id, mp_status) values (?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into a_module_permissions (moduleMst_mod_id, mp_created_by, mp_created_date, mp_mod_id, mp_modified_date, mp_perm_id, mp_status) values (?, ?, ?, ?, ?, ?, ?)

在这个问题中,mp_mod_id将被插入0.即映射表在模块表中没有新插入模块的id。

但是当我尝试像上面那样更新时,它的工作非常完美。

这里有什么问题?!?

编辑: ModuleMst.java

@OneToMany(cascade=CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name="mp_mod_id")
private Set<ModulePermissionMpg> modulePermissionMpg;

ModulePermissionMpg.java

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="mp_mod_id", insertable=false, updatable=false)
private ModuleMst moduleMst;

使用它可以在插入时工作,但是当我进行查询以获取具有该特定mod_id的权限时,它会产生以下错误

Hibernate: select moduleperm0_.mp_id as mp_id1_3_, moduleperm0_.mp_mod_id as mp_mod_i4_3_, moduleperm0_.mp_created_by as mp_creat2_3_, moduleperm0_.mp_created_date as mp_creat3_3_, moduleperm0_.mp_modified_date as mp_modif5_3_, moduleperm0_.mp_perm_id as mp_perm_6_3_, moduleperm0_.mp_status as mp_statu7_3_ from a_module_permissions moduleperm0_ where moduleperm0_.mp_status<>? and moduleperm0_.mp_mod_id=?
java.lang.ClassCastException: com.imobdev.hibernate.entity.ModuleMst_$$_javassist_9 cannot be cast to javassist.util.proxy.Proxy
    at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.getProxy(JavassistLazyInitializer.java:147)
    at org.hibernate.proxy.pojo.javassist.JavassistProxyFactory.getProxy(JavassistProxyFactory.java:75)
    at org.hibernate.tuple.entity.AbstractEntityTuplizer.createProxy(AbstractEntityTuplizer.java:771)
    at org.hibernate.persister.entity.AbstractEntityPersister.createProxy(AbstractEntityPersister.java:4613)
    at org.hibernate.event.internal.DefaultLoadEventListener.createProxyIfNecessary(DefaultLoadEventListener.java:350)
    at org.hibernate.event.internal.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:271)
    at org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:151)
    at org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:1070)
    at org.hibernate.internal.SessionImpl.internalLoad(SessionImpl.java:989)
    at org.hibernate.type.EntityType.resolveIdentifier(EntityType.java:716)
    at org.hibernate.type.EntityType.resolve(EntityType.java:502)
    at org.hibernate.engine.internal.TwoPhaseLoad.doInitializeEntity(TwoPhaseLoad.java:170)
    at org.hibernate.engine.internal.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:144)
    at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:1115)
    at org.hibernate.loader.Loader.processResultSet(Loader.java:973)
    at org.hibernate.loader.Loader.doQuery(Loader.java:921)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:355)
    at org.hibernate.loader.Loader.doList(Loader.java:2554)
    at org.hibernate.loader.Loader.doList(Loader.java:2540)
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2370)
    at org.hibernate.loader.Loader.list(Loader.java:2365)
    at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:497)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:387)
    at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:236)
    at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1264)
    at org.hibernate.internal.QueryImpl.list(QueryImpl.java:103)
    at com.imobdev.dao.ImderpDaoImpl.getOldPermissions(ImderpDaoImpl.java:659)
    at com.imobdev.service.ImderpServiceImpl.getOldPermissions(ImderpServiceImpl.java:183)
    at com.imobdev.controller.ErpController.updateModule(ErpController.java:354)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:777)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:706)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:943)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:868)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at com.imobdev.controller.SessionFilter.doFilter(SessionFilter.java:43)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:423)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1079)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:625)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:318)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)

编辑: ModuleMst.java

@OneToMany(mappedBy="moduleMst", cascade=CascadeType.ALL, fetch = FetchType.LAZY)
private Set<ModulePermissionMpg> modulePermissionMpg;

ModulePermissionMpg.java

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="mp_mod_id")
private ModuleMst moduleMst;

删除可插入和可更新的属性后,出现以下错误

org.hibernate.MappingException: Repeated column in mapping for entity: com.imobdev.hibernate.entity.ModulePermissionMpg column: mp_mod_id (should be mapped with insert="false" update="false")
    at ...
....

0 个答案:

没有答案