-->
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.  [ 1 post ] 
Author Message
 Post subject: Embeddables in a unique table constraint with annotations
PostPosted: Mon Jan 26, 2009 6:48 am 
Newbie

Joined: Mon Jan 26, 2009 6:14 am
Posts: 3
Hi,

I would like to include all fields of an embeddable class in a table-level unique constraint on an entity using annotations (JPA or Hibernate). Does anyone know if it is possible to reference the embeddable in the @UniqueConstaint definition without having to list all its column names explicitly?


e.g. I have a simple embeddable with only integer fields such as:
Code:
@Embeddable
public class TravellerNumbers {
   @Column
   Integer numAdults = 0;
   
   @Column
   Integer numInfants = 0;
   
   @Column
   Integer numStudents = 0

   ...
}


And an entity which includes it as a field:
Code:
@Entity
public class TripDetails {
      @Id
      Long tripId;     

      @Column
      String modeOfTravel;     

      @Column
      TravellerNumbers travellers;
     
   ...
}



The only way I can see to include the embeddable fields in the unique constraint (covering other entity fields as well) is to list the column names of the embeddable individually:
Code:
@Table(
   uniqueConstraints = {
         @UniqueConstraint(
            columnNames = {"modeOfTravel", "numAdults", "numInfants", "numStudents", ...})
   }
)


While this gives the correct effect it seems a bit unsafe - e.g. if anyone changes the embeddable class they'll need to remember to change the column names of all entities that use it.

Does anyone know if there's a "nicer" way to do it with the annotations syntax, e.g. referencing the "travellers" field as a whole?

(A pretty minor problem admittedly, but I'm just curious because it's cropped up a few times recently in my work)

Thanks,
Stephen


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

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.