-->
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: Using Validator with inheritance hierarchy
PostPosted: Wed May 07, 2014 6:43 am 
Newbie

Joined: Wed May 07, 2014 6:25 am
Posts: 1
I have an issue at the moment with using Hibernate validator on an object with an inheritance hierarchy. In the bean class in my constraint-mapping.xml I'm putting the top level class. So imagine a simple hierarchy where Car extends WheeledVehicle but the 'wheels' property is actually on the base class (WheeledVehicle). So my constraint mapping might look something like this

<bean class="com.foo.Car" >
<field name="wheels" >
<constraint annotation="javax.validation.constraints.NotNull"/>
</field>
</bean>

Now when I edit the config file I can see IntelliJ complaining that wheels doesn't exist on Car (because it exists on the base class instead). But if I go to test this then (if the wheels property is null) no validation error is generated and I'm assuming this is because it uses the containsDeclaredField(..) method on ReflectionHelper and 'wheels' isn't declared on Car it is declared on WheeledVehicle. But if I change the bean class to com.foo.WheeledVehicle it doesn't work either because the class I'm actually trying to validate is a Car (and the validator doesn't seem to also apply validation to other classes in the hierarchy).

Is there any way to get the behaviour I desire? (It seems the main issue is the use of getDeclaredFields instead of getField which means only the fields actually declared on the class itself are visible and not the baseclass.) Basically, I want to validate an object at the Car level but I want the validator to be aware of properties contained in the superclass. (I hope that makes sense!)


Regards,


John


Top
 Profile  
 
 Post subject: Re: Using Validator with inheritance hierarchy
PostPosted: Fri May 09, 2014 5:44 am 
Hibernate Team
Hibernate Team

Joined: Sat Jan 24, 2009 12:46 pm
Posts: 388
Hi John,

Constraints declared on super-classes are definitely taken into account when validating an instance of a sub-type. So I'm not sure why it doesn't work when you declared the constraint on the field for WheeledVehicle (it makes sense that it didn't work when specifying Car as class as it doesn't have that field; you could declare it via the "getter" element though, provided you override getWheels() in Car).

Does it work when you declare the constraint using an annotation instead of XML? If so, there may be a problem with the application of XML-defined constraints.

--Gunnar

_________________
Visit my blog at http://musingsofaprogrammingaddict.blogspot.com/


Top
 Profile  
 
 Post subject: Re: Using Validator with inheritance hierarchy
PostPosted: Fri Mar 27, 2015 9:10 am 
Newbie

Joined: Fri Mar 27, 2015 6:55 am
Posts: 1
Hi,

we tried the same with annotations (We're using EJB3 in JBoss 5, Hibernate 3.2.6), and we have a strange effect...

I try to translate our problemn to that WheeledVehicle example:

Code:
@Entity
@Table(name = "VEHICLES")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "VEHI_DTYPE", discriminatorType = DiscriminatorType.STRING)
@DiscriminatorValue("WheeledVehicle")
public class WheeledVehicle
{
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "VEHI_ID_GENERATOR")
    @SequenceGenerator(name = "ORD_ID_GENERATOR", sequenceName = "VEHI_ID_SEQ", allocationSize = 60)
    @Digits(integerDigits = 12, fractionalDigits = 0)
    private Long vehiId;

    @Column
    private Long vehiWheels;

}


and we have two derived Entities:

Code:
@Entity
@DiscriminatorValue("Bike")
public class Bike extends WheeledVehicle
{
    @Column
    private String vehiBrakesBrand;
}


and a Car where we want to add a NotNull Validator to the wheels attriibute:

Code:
@Entity
@DiscriminatorValue("Car")
public class Bike extends WheeledVehicle
{
    @Column
    @NotNull
    private Long vehiWheels;
}


That causes a strange effect, when we try to persist a Bike with vehiWheels = null we receive an error message (see excerpt from StackTrace below)..

Does anyone have an idea why this happens?

Best Regards

Hans



Caused by: org.hibernate.PropertyValueException: not-null property references a null or transient value: Bike.vehiWheels
at org.hibernate.engine.Nullability.checkNullability(Nullability.java:95)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:312)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:203)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:143)
at org.hibernate.ejb.event.EJB3PersistEventListener.saveWithGeneratedId(EJB3PersistEventListener.java:49)
at org.hibernate.event.def.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:154)
at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:110)
at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:61)
at org.hibernate.impl.SessionImpl.firePersist(SessionImpl.java:648)
at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:622)
at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:626)
at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:220)
... 189 more


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.