-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: RuntimeException in Callback Methods
PostPosted: Fri Aug 18, 2006 8:15 am 
Newbie

Joined: Sun Aug 06, 2006 8:54 am
Posts: 15
Hibernate version: Hibernate-3.2 RC2
Name and version of the database: MySQL 5.0.24


Hi. I'm trying to validate data in @PrePersist callback method (same pb as http://forum.hibernate.org/viewtopic.php?t=955614&highlight=callback). Here is my entity bean Category (simplified with no getters/setters) :

Code:
@Entity
@Table(name = "t_category")
public class Category {

    @Id
    @GeneratedValue
    private Long id;
    @Column(nullable = false, length = 30)
    private String name;
    @Column(nullable = false)
    private String description;
    @OneToMany(mappedBy = "category", cascade = CascadeType.REMOVE)
    @OrderBy("name ASC")
    private Collection<Product> products;

    @PrePersist
    private void validateData() {
        if (name == null || "".equals(name))
            throw new ValidationException("Invalid name");
        if (description == null || "".equals(description))
            throw new ValidationException("Invalid description");
    }


ValidationException is a RuntimeException as defined in the spec. Now, I wrote a JUnit class test that tries to persist a Category with invalid values and expects a ValidationException :

Code:
   
    @Test
    public void invalidValues() throws Exception {
        Category category;

        try {
            category = new Category("", "");
            tx.begin();
            em.persist(category);
            fail("Object with empty values should not be created");
        } catch (ValidationException e) {
        }
}


The call back method validateData is called, the transaction is rollbaked, but I do not catch a ValidationException but a RuntimeException (caused by a java.lang.reflect.InvocationTargetException). In fact, if I catch the RuntimeException and write e.getCause().getCause() I will get my ValidationException. Any idea ?

Here is the stack trace

Full stack trace of any exception that occurs:
java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at org.hibernate.ejb.event.BeanCallback.invoke(BeanCallback.java:25)
at org.hibernate.ejb.event.EntityCallbackHandler.callback(EntityCallbackHandler.java:78)
at org.hibernate.ejb.event.EntityCallbackHandler.preCreate(EntityCallbackHandler.java:47)
at org.hibernate.ejb.event.EJB3PersistEventListener.invokeSaveLifecycle(EJB3PersistEventListener.java:31)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:176)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:108)
at org.hibernate.event.def.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:131)
at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:87)
at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:38)
at org.hibernate.impl.SessionImpl.firePersist(SessionImpl.java:620)
at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:594)
at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:598)
at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:186)
at com.yaps.petstore.domain.CategoryTest.invalidValues(CategoryTest.java:132)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at org.junit.internal.runners.TestMethodRunner.executeMethodBody(TestMethodRunner.java:99)
at org.junit.internal.runners.TestMethodRunner.runUnprotected(TestMethodRunner.java:81)
at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34)
at org.junit.internal.runners.TestMethodRunner.runMethod(TestMethodRunner.java:75)
at org.junit.internal.runners.TestMethodRunner.run(TestMethodRunner.java:45)
at org.junit.internal.runners.TestClassMethodsRunner.invokeTestMethod(TestClassMethodsRunner.java:71)
at org.junit.internal.runners.TestClassMethodsRunner.run(TestClassMethodsRunner.java:35)
at org.junit.internal.runners.TestClassRunner$1.runUnprotected(TestClassRunner.java:42)
at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34)
at org.junit.internal.runners.TestClassRunner.run(TestClassRunner.java:52)
at junit.framework.JUnit4TestAdapter.run(JUnit4TestAdapter.java:32)
at com.intellij.rt.execution.junit2.JUnitStarter.main(JUnitStarter.java:32)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:90)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at org.hibernate.ejb.event.BeanCallback.invoke(BeanCallback.java:22)
... 39 more
Caused by: com.yaps.petstore.exception.ValidationException: Invalid name
at com.yaps.petstore.domain.Category.validateData(Category.java:59)
... 44 more


Thanks,

Antonio


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 18, 2006 6:22 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Test on SVN trunk, I think I fixed that one

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Sat Aug 19, 2006 4:37 pm 
Newbie

Joined: Sun Aug 06, 2006 8:54 am
Posts: 15
Fantastic, it works fine.

Thanks.

Antonio


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.