-->
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.  [ 4 posts ] 
Author Message
 Post subject: @CollectionOfElements annotation problem
PostPosted: Fri Aug 03, 2007 5:08 pm 
Newbie

Joined: Fri Aug 03, 2007 4:59 pm
Posts: 4
Hibernate version: 3.2.1

Name and version of the database you are using: MSSQL server 2005

Hello All,
I have a simple entity called StaticStringDatasource. In this entity I have a list of string values which I want to save to a database. I used the collections annotation to do it.


Code:
@Entity
@Table(name = "Datasources_StaticString")
public class StaticStringDatasource extends Datasource {

    /**
     * List of values associated with the StaticStringDatasource.
     */
    @CollectionOfElements
    @JoinTable(name = "Datasources_StaticString_Values")
    @Size(min = 1)
    @Column(nullable = false)
    private List<String> dataValues;

   //getters and setters...
}



What I am trying to achieve is that when ever the static string datasource is saved the list of string values associated with it should also get saved.

If the supplied list is null and exception must be thrown. Now I think the annotation @Column should attain this, but it doesn't seem to be doing it.

If I supply a null list of string values it still works and no error is thrown. Please help I don't know what I am doing wrong.

_________________
zbhiwandiwala


Top
 Profile  
 
 Post subject:
PostPosted: Sat Aug 04, 2007 6:12 am 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
The problem is that even though the list is null there is no corresponding database column that is set to null. Having a null list simple results in zero rows being inserted.

If you're using an entity manager (JPA) you can add validation via annotations either as an event listener or code based validator:
Code:
public class StaticStringDatasource extends Datasource {
// Code based validator...
   @Transient
   @AssertTrue
   private boolean isValid() {
      return dataValues != null && dataValues.size() > 0;
   }

// or event listener...
   @PrePersist
   @PreUpdate
   private void validateObject() {
      if (dataValues == null ||
            dataValues.size() == 0) {
         throw new MyValidationException("The data values list must contain values");
      }
   }
...


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 09, 2007 11:48 am 
Newbie

Joined: Fri Aug 03, 2007 4:59 pm
Posts: 4
Hey,
I managed to validate the data using a code based validator. I put the isValid() method in my base class and also have the method overriden in my sub classes.

In my base class for the @AssertTrue annotation I did not have a message attribute defined. But I do in my sub classes.

The message is by default just read from the base class. So I don't get back the custom message I have defined in each of my subclasses.

Is there a way to get that message?

_________________
zbhiwandiwala


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 09, 2007 11:48 am 
Newbie

Joined: Fri Aug 03, 2007 4:59 pm
Posts: 4
Hey,
I managed to validate the data using a code based validator. I put the isValid() method in my base class and also have the method overriden in my sub classes.

In my base class for the @AssertTrue annotation I did not have a message attribute defined. But I do in my sub classes.

The message is by default just read from the base class. So I don't get back the custom message I have defined in each of my subclasses.

Is there a way to get that message?

_________________
zbhiwandiwala


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.