-->
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: enum with foreign-key constraints
PostPosted: Sun Oct 12, 2008 11:19 am 
Newbie

Joined: Sun Oct 12, 2008 8:24 am
Posts: 1
Hi

I would like to check the used enums not only on the application layer
but also on the database. To ensure that all entries that are added
manually to the database are valid too. Therefor I've tried to use a
@ManyToOne mapping to ensure that hibernate creates the needed
foreign-key constraints.

Code:
@Entity
@Immutable
public enum Foo {
   A("foobar", 1, 3L);

   @Id
   public final String value;

   public final int value2;

   @Transient
   public final long value3;

   private Foo(String value, int value2, long value3)
      this.value = value;
      this.value2 = value2;
      this.value3 = value3;
   }
}

@Entity
public class Bar {

   @Id
   private Long id;

   @ManyToOne
   @ForeignKey(name = "bar_foo_fkey")
   private Foo foo;
}


Needless to say, that Hibernate is not able to retrieve Foo objects, as
an Exception (No default constructor for entity: Foo) occurs. Therefore
I've implemented a custom UserType that mapps from the String value to
an Foo enum. But I'm not able to able to combine @Type(type =
"FooUserType") with the @ManyToOne Attribute.

So at the Moment I've got the choice to use @Type or have Hibernate
generate foreign-key constraints via @ManyToOne. Who can I combine both?

thx
Mathias
Code:


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.