Okay. I think I'm starting to grasp what you want.
You have a store and each store is of a certain type? So you have stores of the type
"department store", "hardware store" etc.
I guess there are fewer types of stores than stores. If that is so you do not have a
@OneToOne relationship but @ManyToOne: Many stores belong to one store type. In SQL
terminology this would be a 1:n relationship between types and stores.
If that is the case you can simply do this (leaving aside your table_name problem for a
moment):
| public class Store {
| private Type storeType;
| @ManyToOne
| @JoinColumn(name = "STORE_TYPE_ID")
| public Type getStoreType() {
| return storeType;
| }
| //
| }
|
If you set up the Type class correctly the persistence layer will automatically determine
that the join column must refer to TYPE.TYPE_ID and add a constraint to enforce this
foreign key relationship if necessary.
Just to make this complete: I would strongly recommend that you drop your requirement of
having the table_name in your type table. (I assume this is not really a table_name for
use in dynamic sql but a string value to identify the "type of type" :-)) If you
want this it would require you to use composite keys which is IMHO painful.
But THEORETICALLY you could do it like this: You create a class TypePK for the primary key
of Type with a field for TYPE_ID (Integer) and a String for TABLE_NAME and annotate the
class with @Embeddable, then you replace the type_id and table_name properties with a
single property of the TypePk class and annotate it with @EmbeddedId.
Then you change your @ManyToOne relationship to something like:
@ManyTo
@JoinColumns({
@JoinColumn(name="STORE_TYPE_ID",
referencedColumnName="TYPE_ID"),
@JoinColumn(name="TABLE_NAME",
referencedColumnName="TABLE_NAME")
})
Finally adjust the setter of your Store to prevent setting types that do not have
"STORE" as the table_name property of the type's typePK.
Sounds complicated? It is - and in my opinion not worth the effort.
Regards
Felix
P.S.: BTW: If the relationship between store and type is really 1:1 then you could solve
this more elegantly by using the inheritance strategy joined.
View the original post :
http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4034352#...
Reply to the post :
http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&a...