Hello!
I have entity Answer as :
Code:
@Entity
@Table(name = DataBaseConstants.ANSWER_TABLE)
public class Answer extends Post<Long> {
private static final long serialVersionUID = 639804536588328311L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = DataBaseConstants.ANSWER_ID, nullable = false, updatable = false, insertable = false)
private Long answer_id;
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "topic", referencedColumnName = "topic_id")
private Topic topic;
// @LazyToOne(LazyToOneOption.NO_PROXY)
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "user", referencedColumnName = "user_id")
private JuniorUser user;
public Answer() {
}
public Answer(Long answer_id) {
this.answer_id = answer_id;
}
public Answer(Long answer_id, Long version) {
this.answer_id = answer_id;
setVersion(version);
}
public Answer(Long answer_id, Integer rate, Date publish_date) {
this.answer_id = answer_id;
super.setRate(rate);
super.setPublish_date(publish_date);
}
public Answer(String content, Integer rate, Date date) {
super(content, rate, date);
}
public Long getAnswer_id() {
return answer_id;
}
public void setAnswer_id(Long answer_id) {
this.answer_id = answer_id;
}
@Transient
@Override
public Long getId() {
return getAnswer_id();
}
public Topic getTopic() {
return topic;
}
public void setTopic(Topic topic) {
this.topic=topic;
}
public JuniorUser getUser() {
return user;
}
public void setUser(JuniorUser user) {
this.user = user;
}
@Override
public String toString() {
ToStringHelper helper = Objects.toStringHelper(getClass().getCanonicalName());
helper.add("id", getId());
helper.add("publish_date", getPublish_date());
helper.add("rate", getRate());
return helper.toString();
}
}
I also have AnswerDao and AnswerService.
AnswerService is intercepted by spring @Aspect.
Hibernate session's methods as save,update,get for Answer work successfully.
Unfortunately method org.hibernate.Session.delete(Answer answer) throws the following excpetion:
Code:
java.lang.NullPointerException
at org.hibernate.event.internal.DefaultFlushEntityEventListener.isUpdateNecessary(DefaultFlushEntityEventListener.java:240)
at org.hibernate.event.internal.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:163)
at org.hibernate.event.internal.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:227)
at org.hibernate.event.internal.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:99)
at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51)
at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1233)
at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:403)
at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101)
at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:175)
at org.springframework.orm.hibernate4.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:554)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:755)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:724)
at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:475)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:270)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:94)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:91)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:631)
at com.javahelp.service.AnswerService$$EnhancerByCGLIB$$be3aa647.delete(<generated>)
at test.javahelp.dao.GenericServiceTest.doDelete(GenericServiceTest.java:235)
at test.javahelp.dao.answer.AnswerServiceTestHibernate.testDelete(AnswerServiceTestHibernate.java:72)
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:601)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
at test.javahelp.dao.runner.GenericServiceTestRunner.run(GenericServiceTestRunner.java:29)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
I debugged a lot and found out that exception is thrown in
org.hibernate.bytecode.instrumentation.internal.javassist.JavassistHelper.extractFieldInterceptor(Object entity) method:
Code:
public static FieldInterceptor extractFieldInterceptor(Object entity) {
return ( FieldInterceptor ) ( ( FieldHandled ) entity ).getFieldHandler();
}
I was in that method and saw 'entity' argument and it was not null and it held real,true properties of my answer object.
Why is here NullPointerException?
Thank you!
P.S. Answer's superclass also has @Version property. If I remove that property then delete works successfully but I don't want to remove @Version property.