-->
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.  [ 5 posts ] 
Author Message
 Post subject: Mapping static fields or something like that
PostPosted: Thu Jul 17, 2008 10:35 am 
Newbie

Joined: Fri May 23, 2008 5:43 am
Posts: 13
Location: Switzerland
Hi,

I would like do have some static fields in my entity which will populated with some domain objects. For example fields for default category - when new entity is created it will automatically assign this default category to category field.

class Product {
public static Category defaultCategory1 ;

@Column
Category category ;

public Product(boolean isNew) {
if(isNew) {
category = defaultCategory1 ;
}
}

}

This is oversimplified example. In reality I can have complicated logic for many fields and and may be some related entities will be generated.

Right now I, off cause, can assign the category for the new entity from outside for each new entity or do one assignment for the static field.

So, my code will alway look like this:
Product.initializeDefaults(entityManager) ; //or whatever method I choose
// to initialize the static fields
Product p = new Product(true) ;


Does anyone have any suggestions about this logic? Can Hibernate automate any of this or it must be manual for each class?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 17, 2008 1:37 pm 
Newbie

Joined: Thu Jul 17, 2008 1:24 pm
Posts: 9
You can modify the table columns to have a default value.

Also you can use the columnDefinition annotation.

Ex.

@Column(name = “column”, columnDefinition = “bigint(20) default 0″)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 17, 2008 1:40 pm 
Newbie

Joined: Thu Jul 17, 2008 1:24 pm
Posts: 9
dbl post


Last edited by novahokie on Thu Jul 17, 2008 1:46 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 17, 2008 1:41 pm 
Newbie

Joined: Thu Jul 17, 2008 1:24 pm
Posts: 9
dbl post


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 20, 2008 7:54 am 
Newbie

Joined: Fri May 23, 2008 5:43 am
Posts: 13
Location: Switzerland
These are not exactly default values, but the values generated according to some logic. So I have to inject the domain object with these values, preferably once. I want it to be done automatically with some configuration file, similar to the way Hibernate does it with fields of the objects


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