This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Object Relational Mapping and the fly likes using name attribute of Entity Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "using name attribute of Entity" Watch "using name attribute of Entity" New topic
Author

using name attribute of Entity

Vinay Taneja
Greenhorn

Joined: Apr 04, 2012
Posts: 9
Hi,

I have one entity class and want to map it to more than one table eg: Comment is entity class and a comment can be on different entities in the system so for entity A comment will be stored in CommentA table for entity B comment will be stored in CommentB table. now I want to map same Comment class with different table through orm.xml and there I can map same class with different tables with different name attribute.

But now how to persist instance of Entity as in persist method it takes only entity class instance and will store it in which table?
the same way how to update?

I am not looking for MappedSuperClass option.
Emanuel Kadziela
Ranch Hand

Joined: Mar 24, 2005
Posts: 186
As far as I know there is no way to do what you suggest. One Entity = one table, except for special cases like inheritance scenarios, which you indicated you are not interested in.

Perhaps if you describe your actual requirements in more detail, we can suggest an appropriate design/solution.
Vinay Taneja
Greenhorn

Joined: Apr 04, 2012
Posts: 9
I want to develop one component which will perform certain CRUD eg: note or comment. Now note can be placed on many entities in a system and I want to maintain them in different tables for each entity. what I want is that I just configure table name in my orm.xml and use same Comment.class as class and specify that name in my service instance of spring configuration and it should do CRUD based on that name.

Which eventually means I am saying when I use instance A of my service which is configured to use name commentsA for Comment entity it will do CRUD on table_a while when I use instance B of my same service which is configured to use name commentB for Comment, it will work on table_b.

<bean id="instanceA" class="test.MyService">
<constructor-arg value="commentsA"/>
<constructor-arg value="entityMgrFac"/>
</bean>

<bean id="instanceB" class="test.MyService">
<constructor-arg value="commentsB"/>
<constructor-arg value="entityMgrFac"/>
</bean>

<bean id="entityOneService" class="test.EntityOneService">
<constructor-arg value="instanceA"/>
</bean>

<bean id="entityTwoService" class="test.EntityTwoService">
<constructor-arg value="instanceB"/>
</bean>

however one question I have is what is the meaning of having name attribute in entity if we can not use it.
Emanuel Kadziela
Ranch Hand

Joined: Mar 24, 2005
Posts: 186
You're focused on the name attribute, but have you thought about the foreign key mappings? Each comment relates to a specific entity owning the comment, right? This means if you ever want to find comments for any specific entity, you need a foreign key reference to the owning entity, and each comment type will have a foreign key to a different entity (UserComment points to User, EmployeeComment points to Employee, etc.). All of these foreign keys have to be mapped.

I don't believe there is any way to avoid having separate mappings for each Comment. It would be advisable to use the inheritance mappings and have the Comment class be a superclass of all specific comment classes, but I don't see any way of avoiding separate mappings for each specific Comment entity without significantly rewriting or customizing the ORM code itself.
Vinay Taneja
Greenhorn

Joined: Apr 04, 2012
Posts: 9
actually thinking of foreign key would be data centric and I am thinking from componentisation point of view. In actual all the refrential contraints are being managed in database itself, and if I want to fetch entities from reverse direction, I can do it in database and if I know the instance of service I can also configure the correct target entity. Comment is just an example but in real life, cheese is just cheese be it on garlic bread or pizza.

And if we talk about EJB concepts where remoting is possible, lets say CommentsBean is on different server and I am putting comment on my UserBean so I need not to maintain any reference to entity in my User entity and viceversa.
I don't want to design that if I fetch one Entity all the data of my database comes with it. Like if I fetch one user, all the comments are linked with it in entity and all the other work and entities which are associated with that user and then any shared entity where user A worked and user B too, B is also linked through that shared entity path. I mean a user is a user only.

So its like, I want to create one component which can store data in different tables based on configuration. I think it was possible with normal old EJB concepts.
Emanuel Kadziela
Ranch Hand

Joined: Mar 24, 2005
Posts: 186
I don't know much about EJBs, but I do know that the ORM layer needs to be aware of the relationships between entities corresponding to the relationships in the database. Managing associations is one of its main advantages.

Relationships in your entities and ORM mappings can be one-directional or bi-directional. No one says that a User object needs to be aware of all its comment objects. The UserComment can have a one-directional reference to the User mirroring the underlying foreign key reference, but the User can be completely unaware of Comments or UserComments. That's one of the reasons why fetching one entity does not bring the whole database with it. Lazy loading is another.

If a comment is just a comment and cheese is just cheese, why do you want to put your comments in different tables? If you don't care where the comment came from, just as you don't care which pizza your cheese ended up on, then just put them in one table. But as soon as you start caring about which pizza got which slice of cheese, or who made what comment, and you start making that differentiation in the database, you need to reflect it in your ORM mappings, or modify the ORM code itself.
 
 
subject: using name attribute of Entity
 
Similar Threads
nested java maps
Hibernate: Dynamic Table Mapping
How to map returned cursor to a POJO / Bean in Hibernate?
Passed SCBCD 5.0 :)
@ManyToMany, @ManyToOne, Join Tables