我面临一个非常奇怪的问题,我无法理解。有两个实体“用户”和“联系人”。异常在 ProjectController.java 中被抛出user.getContactSet().add(contact);
我正在尝试在“用户”和“联系人”之间建立多对多关系
用户.java
public class User {
private Integer userID;
private String userLoginEmail;
private String password;
private String userFirstName;
.
.
联系方式.java
public class Contact {
private Integer contactID;
private String givenName;
private String familyName;
private String telephoneNumber;
private String cellNumber;
.
.
用户.hbm.xml
<hibernate-mapping package="com.smallworks.model" schema="smallworksdb">
<class name="User" table="USERACCOUNT">
<id column="USER_ID" length="500" name="userID">
<generator class="increment" />
</id>
<property column="USER_LOGIN_EMAIL" generated="never" lazy="false" length="100" name="userLoginEmail" />
<property column="USER_PASSWORD" generated="never" lazy="false" length="50" name="password" />
<property column="ACTIVATION_CODE" generated="never" lazy="false" length="100" name="activationCode" />
<property column="ACCOUNT_ACTIVATED" generated="never" lazy="false" length="1" name="accountActivated" />
<set name="contactSet" table="USER_CONTACT"
inverse="false" lazy="true" fetch="select" cascade="all">
<key column="USER_ID"/>
<many-to-many column="CONTACT_ID" class="Contact"/>
</set>
</class>
联系人.hbm.xml
<hibernate-mapping package="com.smallworks.model" schema="smallworksdb">
<class name="Contact" table="CONTACT">
<id column="CONTACT_ID" length="500" name="contactID">
<generator class="increment" />
</id>
<property column="GIVEN_NAME" generated="never" lazy="false" length="100" name="givenName" />
<property column="FAMILY_NAME" generated="never" lazy="false" length="100" name="familyName" />
<property column="TELEPHONE_NUMBER" generated="never" lazy="false" length="100" name="telephoneNumber" />
<property column="CELL_NUMBER" generated="never" lazy="false" length="100" name="cellNumber" />
<property column="EMAIL" generated="never" lazy="false" length="100" name="email" />
<property column="STREET_NAME" generated="never" lazy="false" length="100" name="streetName" />
<property column="CITY" generated="never" lazy="false" length="100" name="city" />
<property column="STREET_ADDRESS" generated="never" lazy="false" length="100" name="streetAddress" />
<property column="ADDRESS_LOCALITY" generated="never" lazy="false" length="100" name="addressLocality" />
<property column="ADDRESS_REGION" generated="never" lazy="false" length="100" name="addressRegion" />
<property column="POSTAL_CODE" generated="never" lazy="false" length="100" name="postalCode" />
<property column="COMMENT" generated="never" lazy="false" length="100" name="comment" />
<!-- many to many mapping with the User via User_Contact table -->
<set name="userSet" table="USER_CONTACT"
inverse="true" lazy="true" fetch="select">
<key column="USER_ID" />
<many-to-many column="CONTACT_ID" class="Contact" />
</set>
</class>
项目控制器.java
@RequestMapping(value = { "/project/addContact.do" }, method = RequestMethod.POST)
@ResponseBody
@JsonIgnore
public String addContact(@ModelAttribute("UserContactAction") ContactAction contactAction, BindingResult result, HttpServletRequest request, Model model) {
User user=(User) request.getSession().getAttribute("user");
if(contactAction!=null){
if(contactAction.getContact()!=null){
Contact contact=contactAction.getContact();
if(contact.getUserSet()==null){
contact.setUserSet(new HashSet<User>());
}
user.getContactSet().add(contact); //ERROR Gets thrown here
}
}else{
System.out.println("userContactAction is NULL");
}
例外:
01:13:26.953 [http-bio-8080-exec-9] DEBUG o.s.w.b.a.s.HandlerMethodInvoker - Invoking request handler method: public java.lang.String com.smallworks.controller.ProjectController.addContact(com.smallworks.model.view.ContactAction,org.springframework.validation.BindingResult,javax.servlet.http.HttpServletRequest,org.springframework.ui.Model)
01:13:26.961 [http-bio-8080-exec-9] ERROR o.h.LazyInitializationException - failed to lazily initialize a collection of role: com.smallworks.model.User.contactSet, no session or session was closed
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.smallworks.model.User.contactSet, no session or session was closed
at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:380) [hibernate-core-3.3.1.GA.jar:3.3.1.GA]
at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:372) [hibernate-core-3.3.1.GA.jar:3.3.1.GA]
at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:365) [hibernate-core-3.3.1.GA.jar:3.3.1.GA]
at org.hibernate.collection.PersistentSet.add(PersistentSet.java:212) [hibernate-core-3.3.1.GA.jar:3.3.1.GA]
at com.smallworks.controller.ProjectController.addContact(ProjectController.java:96) [ProjectController.class:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [na:1.6.0_33]
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) [na:1.6.0_33]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) [na:1.6.0_33]
at java.lang.reflect.Method.invoke(Unknown Source) [na:1.6.0_33]
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176) [spring-web-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:426) [spring-webmvc-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:414) [spring-webmvc-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790) [spring-webmvc-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719) [spring-webmvc-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644) [spring-webmvc-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560) [spring-webmvc-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:647) [servlet-api.jar:na]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728) [servlet-api.jar:na]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305) [catalina.jar:7.0.33]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) [catalina.jar:7.0.33]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222) [catalina.jar:7.0.33]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123) [catalina.jar:7.0.33]
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472) [catalina.jar:7.0.33]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171) [catalina.jar:7.0.33]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99) [catalina.jar:7.0.33]
我很困惑,为什么会出现这个异常,任何形式的帮助都将不胜感激。我还尝试将用户和联系人带走,而是尝试在员工和会议之间做多对多(只是创建了一些独立的对象),然后一切正常。请帮忙...