ryekayo ryekayo - 7 months ago 17
Java Question

Unknown entity: java.lang.Long in JUnit test

I am trying to create a JUnit test case that tests two overridden methods. I have narrowed down the problem to the exact line of code but cannot for the life of me figure out why this stack trace is occurring. Here is the method in my class:

@Override
public void deleteUserTicketEntity(long ticketNumber)
{
//UserTicketEntity deleteTicket = (UserTicketEntity) this.sessionFactory.getCurrentSession().get(UserTicketEntity.class, ticketNumber);
this.sessionFactory.getCurrentSession().delete(ticketNumber);
}


Here is the test Method in my JUnit class:

@Test
public void testDeleteTicket() throws Exception
{
long Id = 4;
long _Id = 87;
UserTicketEntity ticket = userTicketDao.getUserTicketEntity(Id);
assertNotNull(ticket);
System.out.println("testDeleteTicket: START");
// ------------------------------------------------------------------------------
userTicketDao.deleteUserTicketEntity(ticket);
System.out.println("testDeleteTicket: ticket deleted");
// ------------------------------------------------------------------------------
System.out.println("testDeleteTicket: FINISHED");
// ------------------------------------------------------------------------------
long _ticketNumber = 1;
UserTicketEntity ticketTwo = userTicketDao.getUserTicketEntity(_Id);
assertNotNull(ticketTwo);
assertNotNull(_ticketNumber);
System.out.println("testDeleteUserTicketEntity: START");
userTicketDao.deleteUserTicketEntity(_ticketNumber);
System.out.println("testDeleteUserTicketEntity: ticket deleted");
// ------------------------------------------------------------------------------
System.out.println("testDeleteUserTicketEntity: FINISHED");
}


I have narrowed down that the line of code in question is:

userTicketDao.deleteUserTicketEntity(_ticketNumber);


And here is the stacktrace:

Tests run: 5, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 1.863 sec <<< FAILURE!
testDeleteTicket(com.ticketingsystem.dao.UserTicketDAOTest) Time elapsed: 0.03 sec <<< ERROR!
org.hibernate.MappingException: Unknown entity: java.lang.Long
at org.hibernate.internal.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:776)
at org.hibernate.internal.SessionImpl.getEntityPersister(SessionImpl.java:1533)
at org.hibernate.event.internal.DefaultDeleteEventListener.onDelete(DefaultDeleteEventListener.java:82)
at org.hibernate.event.internal.DefaultDeleteEventListener.onDelete(DefaultDeleteEventListener.java:56)
at org.hibernate.internal.SessionImpl.fireDelete(SessionImpl.java:924)
at org.hibernate.internal.SessionImpl.delete(SessionImpl.java:863)
at com.ticketingsystem.dao.UserTicketDAOImpl.deleteUserTicketEntity(UserTicketDAOImpl.java:55)
at com.ticketingsystem.dao.UserTicketDAOTest.testDeleteTicket(UserTicketDAOTest.java:186)
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.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:254)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:89)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:193)
at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:252)
at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:141)
at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112)
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.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)
at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)


Results :

Tests in error:
testDeleteTicket(com.ticketingsystem.dao.UserTicketDAOTest): Unknown entity: java.lang.Long

Tests run: 5, Failures: 0, Errors: 1, Skipped: 0


Does anyone have any suggestions to work around this?

Answer

The hibernate Session.delete(Object object) doesn't delete by ID, it needs an instance of your Entity instead.

Just change your DAO method to accept the object itself or instantiate a new object to delete.

public void deleteUserTicketEntity(UserTicketEntity ticket) {
    // delete passed in object
    this.sessionFactory.getCurrentSession().delete(ticket);
}

public void deleteUserTicketEntityById(long ticketNumber) {
    // instantiate new object w/ ID, then delete
    final UserTicketEntity deleteTicket = new UserTicketEntity();
    deleteTicket.setId(ticketNumber);
    this.sessionFactory.getCurrentSession().delete(deleteTicket);
}
Comments