开发者社区> 问答> 正文

Hibernate下一个类同时与两个类单向多对一无法保存实体?报错

用户、贴吧、帖子三个类

帖子和用户单向多对一

帖子和贴吧单向多对一

保存帖子时报错:

HTTP Status 500 -


type Exception report

message

description The server encountered an internal error that prevented it from fulfilling this request.

exception

java.lang.reflect.InvocationTargetException
	sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
	sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	java.lang.reflect.Method.invoke(Method.java:606)
	com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:453)
	com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:292)
	com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:255)
	org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:256)
	com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
	com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:176)
	com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
	com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
	com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:265)
	org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)
	com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
	com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
	com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:138)
	com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
	com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:236)
	com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
	com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
	com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:236)
	com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
	com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
	com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:190)
	com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
	org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:75)
	com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
	org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:90)
	com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
	org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:243)
	com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
	com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:100)
	com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
	com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:141)
	com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
	com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:145)
	com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
	com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:171)
	com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
	com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
	com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:176)
	com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
	org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)
	com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
	com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:192)
	com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
	com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:187)
	com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
	org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:54)
	org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:511)
	org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
	org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)
	org.springframework.orm.hibernate4.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:152)
	org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)

root cause

java.lang.AbstractMethodError: org.apache.commons.dbcp.DelegatingPreparedStatement.setCharacterStream(ILjava/io/Reader;J)V
	sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
	sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	java.lang.reflect.Method.invoke(Method.java:606)
	org.hibernate.engine.jdbc.internal.proxy.AbstractStatementProxyHandler.continueInvocation(AbstractStatementProxyHandler.java:122)
	org.hibernate.engine.jdbc.internal.proxy.AbstractProxyHandler.invoke(AbstractProxyHandler.java:81)
	com.sun.proxy.$Proxy38.setCharacterStream(Unknown Source)
	org.hibernate.type.descriptor.sql.ClobTypeDescriptor$3$1.doBind(ClobTypeDescriptor.java:83)
	org.hibernate.type.descriptor.sql.BasicBinder.bind(BasicBinder.java:92)
	org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:280)
	org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:275)
	org.hibernate.type.AbstractSingleColumnStandardBasicType.nullSafeSet(AbstractSingleColumnStandardBasicType.java:57)
	org.hibernate.persister.entity.AbstractEntityPersister.dehydrate(AbstractEntityPersister.java:2747)
	org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3025)
	org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3469)
	org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.java:88)
	org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:362)
	org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:354)
	org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:275)
	org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:326)
	org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:52)
	org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1213)
	org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:402)
	org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101)
	org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:175)
	org.springframework.orm.hibernate4.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:468)
	org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:755)
	org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:724)
	org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:387)
	org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:120)
	org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
	org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:631)
	com.lxl.action.AddPostAction$$EnhancerByCGLIB$$603a681b.execute(<generated>)
	sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
	sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	java.lang.reflect.Method.invoke(Method.java:606)
	com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:453)
	com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:292)
	com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:255)
	org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:256)
	com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
	com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:176)
	com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
	com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
	com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:265)
	org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)
	com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
	com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
	com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:138)
	com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
	com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:236)
	com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
	com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
	com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:236)
	com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
	com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
	com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:190)
	com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
	org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:75)
	com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
	org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:90)
	com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
	org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:243)
	com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
	com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:100)
	com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
	com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:141)
	com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
	com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:145)
	com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
	com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:171)
	com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
	com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
	com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:176)
	com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
	org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)
	com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
	com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:192)
	com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
	com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:187)
	com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
	org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:54)
	org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:511)
	org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
	org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)
	org.springframework.orm.hibernate4.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:152)
	org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)

note The full stack trace of the root cause is available in the Apache Tomcat/7.0.53 logs.


Apache Tomcat/7.0.53

以下是源代码,为了避免太长,将构造方法等没用的东西删掉了:

/**
 * 帖子
 */
@Entity
@Table(name = "posts")
public class Post {

	private User postUser; // 帖子作者
	private Tieba postTieba; // 帖子所在贴吧

	/**
	 * 获得帖子作者
	 * 
	 * @return 帖子作者
	 */
	@ManyToOne(cascade = {CascadeType.REFRESH,CascadeType.MERGE,CascadeType.PERSIST}, fetch = FetchType.EAGER // 只要是@xxxOne默认fetchType是eager
	, optional = false // 代表这个对象是不是可有可无的
	)
	@JoinColumn(name = "user_name")
	// 指定外键的名称
	public User getPostUser() {
		return postUser;
	}

	/**
	 * 设置帖子作者
	 * 
	 * @param postUser
	 *            帖子作者
	 */
	public void setPostUser(User postUser) {
		this.postUser = postUser;
	}

	/**
	 * 获得帖子所在贴吧
	 * 
	 * @return 帖子所在贴吧
	 */
	@ManyToOne(cascade = {CascadeType.REFRESH,CascadeType.MERGE,CascadeType.PERSIST}, fetch = FetchType.EAGER // 只要是@xxxOne默认fetchType是eager
	, optional = false // 代表这个对象是不是可有可无的
	)
	@JoinColumn(name = "tieba_name")
	// 指定外键的名称
	public Tieba getPostTieba() {
		return postTieba;
	}

	/**
	 * 设置帖子所在贴吧
	 * 
	 * @param postTieba
	 *            帖子所在贴吧
	 */
	public void setPostTieba(Tieba postTieba) {
		this.postTieba = postTieba;
	}

}



/**
 * 用户
 */
@Entity
@Table(name = "users")
public class User {

	private Set<Tieba> favourateTiebas = new HashSet<Tieba>(); // 用户喜欢的贴吧

	/**
	 * 获得该用户关注的贴吧
	 * 
	 * @return 该用户关注的贴吧
	 */
	@ManyToMany(cascade = {CascadeType.REFRESH,CascadeType.MERGE})
	@JoinTable(name = "user_tieba", inverseJoinColumns = @JoinColumn(name = "tieba_name") // 可以理解为“跟被维护端的外键的定义”
	, joinColumns = @JoinColumn(name = "user_name"))
	// 注意这个name是没有提示的
	public Set<Tieba> getFavourateTiebas() {
		return favourateTiebas;
	}

	/**
	 * 设置该用户关注的贴吧
	 * 
	 * @param favourateTiebas
	 *            该用户关注的贴吧
	 */
	public void setFavourateTiebas(Set<Tieba> favourateTiebas) {
		this.favourateTiebas = favourateTiebas;
	}
}



/**
 * 贴吧
 */

@Entity
@Table(name = "tiebas")
public class Tieba {
	private Set<User> thisTiebasUsers = new HashSet<User>(); // 本贴吧的用户

	/**
	 * 获得该贴吧所有用户
	 * 
	 * @return 该贴吧所有用户
	 */
	@ManyToMany(cascade = {CascadeType.REFRESH,CascadeType.MERGE} // 笔记:级联删除基本用不到
	, mappedBy = "favourateTiebas")
	public Set<User> getThisTiebasUsers() {
		return thisTiebasUsers;
	}

	/**
	 * 设置该贴吧所有用户
	 * 
	 * @param thisTiebasUsers
	 *            该贴吧所有用户
	 */
	public void setThisTiebasUsers(Set<User> thisTiebasUsers) {
		this.thisTiebasUsers = thisTiebasUsers;
	}
}



/**
 * 帖子_添加帖子Action
 */
@Controller
@Transactional
public class AddPostAction {
	@Resource
	PostService postService;// 注入帖子Service
	@Resource
	UserService userService;// 注入用户Service
	@Resource
	TiebaService tiebaService;// 注入贴吧Service
	private String userName;//用户名
	private String tiebaName;//贴吧名
	private String postTitle;//帖子标题
	private String postContent;//帖子内容

	/**
	 * 调用本Action时执行本方法
	 * 
	 * @return 用于Action判断跳转方向的字符串
	 */
	public String execute() {
		User user=userService.find(userName);
		Tieba tieba=tiebaService.find(tiebaName);
		Post post=new Post(postTitle, postContent, user, tieba);
		postService.add(post);
		return "success";
	}
}



求助!卡了一礼拜没解决了

展开
收起
爱吃鱼的程序员 2020-06-14 17:20:19 398 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    可能是数据库驱动的问题,换个驱动版本试试<divclass='ref'>

    引用来自“JeffreyLin”的评论

    可能是数据库驱动的问题,换个驱动版本试试<divclass='ref'>

    引用来自“JeffreyLin”的评论

    可能是数据库驱动的问题,换个驱动版本试试<divclass='ref'>

    引用来自“JeffreyLin”的评论

    可能是数据库驱动的问题,换个驱动版本试试

    2020-06-14 17:20:37
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
继承与功能组合 立即下载
低代码开发师(初级)实战教程 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载