-->
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.  [ 3 posts ] 
Author Message
 Post subject: ManyToMany mapping table with extra fields
PostPosted: Wed Apr 23, 2008 2:03 pm 
Newbie

Joined: Thu Sep 20, 2007 1:33 pm
Posts: 8
Hibernate version:

Maven dependencies:

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-annotations</artifactId>
<version>3.3.0.ga</version>
</dependency>

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>3.3.1.ga</version>
</dependency>


Name and version of the database you are using:

PostgreSQL 8.2/8.3


I am mapping a ManyToMany relationship in my app. Ideally, I want the mapping table to look like this:

Code:
CREATE TABLE map_vehicle_division
(
   unique_id             varchar(32) NOT NULL,
   last_modified         timestamp with time zone NOT NULL,
   vehicle_unique_id     varchar(32) NOT NULL,
   division_unique_id    varchar(32) NOT NULL
) ;


Looking at the docs and after testing, I haven't been able to have the unique_id for the mapping table. I am aware of using a unique primary key, but I am trying to avoid that. Is that possible?

More importantly Is it possible to set the value of last_modified when when the relationship is created? I can live without the unique_id if I have to but I want to set the last_modified field when a new record is created. What's the best way to do that?

Thanks,

L


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 24, 2008 4:00 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Are you sure you want to use Strings for PK? This is not really recommended, but possible.

You want full control of your manyToMany bridge table, so you will need to map an entity representing this table, and use @OneToMany in the Vehicle entity and another @OneToMany in the Division.

About unique_id: in the entity "MapVehicleDivision" choose a Generator,
many different types of generators are available. (see annotations and core docs)

As you have a new entity representing your bride, you may set the last_modified in java code; Alternatively a DB default value would work nice.

You may give a look to "Map" mappings, that could be a good choice for this.

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 30, 2008 2:25 pm 
Newbie

Joined: Thu Sep 20, 2007 1:33 pm
Posts: 8
Thanks for the information. Yes, we do want string IDs for various reasons.

I'll look into mapping. I think the main problem I am having is that the entity manager stores data in all three tables automatically. There is no Java code to manage the storage (i.e. I just call the persist() method an it creates the new bridge entity).

Thanks for the Map information, I will take a look.

L


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