-->
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: multiple object types using one metadata table
PostPosted: Fri Oct 07, 2005 2:43 pm 
Newbie

Joined: Fri Oct 07, 2005 2:21 pm
Posts: 4
Location: New Brunswick, Canada
I'm trying to undertand hibernate, so bear with me. I'm trying to use the ejb3 annotations.

Here's what I want to do:

I want to have multiple object types use the same metadata table to store their info. But I'd like for the metadata table to not care about how many other objects write to it.

Ex:
User object with demographics like name, address, etc.
Org object with demographics like address, phone etc.

I'd like to store those demographics in a meta data table, with the intention of adding other objects to it.

My thought is the metadata table would have a autogen id field as the PK, and an ownerId field as reference to the id from the object table user.id, bank.id) and a ownertype field which would be the class name of the object (com.blah.dao.user or com.blah.dao.org). And then all the other fields.

How do I do the annotations for this?

Here's what I have so far (extra removed for readability):

USER:

@Entity()
@SecondaryTable(name = "metadata", join = {@JoinColumn(name = "ownerid")})
public class User {
private Long id = null;
private Collection<MetaData> metaData = new ArrayList<MetaData>();

@Id(generate = GeneratorType.IDENTITY)
@Column(secondaryTable = "metadata")
public Long getId() {
return this.id;
}

@OneToMany(cascade = CascadeType.ALL, targetEntity = "com.blah.dao.MetaData")
@JoinColumn(name = "userId")
public Collection<MetaData> getMetaData() {
return metaData;
}
}

ORG:

@Entity()
@SecondaryTable(name = "metadata", join = {@JoinColumn(name = "ownerid")})
public class Org {
private Long id = null;
private Collection<MetaData> metaData = new ArrayList<MetaData>();

@Id(generate = GeneratorType.IDENTITY)
@Column(secondaryTable = "metadata")
public Long getId() {
return this.id;
}

@OneToMany(cascade = CascadeType.ALL, targetEntity = "com.blah.dao.MetaData")
@JoinColumn(name = "orgId")
public Collection<MetaData> getMetaData() {
return metaData;
}
}

METADATA:

@Entity(access = AccessType.FIELD)
public class MetaData {
private Long id = null;
private Long ownerId = null;
private String metaName = null;
@Column(length = 1024)
private String metaValue = null;
@Id(generate = GeneratorType.IDENTITY)
public Long getId() {
return this.id;
}
}


I have a feeling the secondary table thing isn't right, but I don't know what is. I don't have the ownertype field in the metadata class. Is that something hibernate can create for me? Do I have to manually put the class name in the field?

thanks!


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.