Solved

Hibernate: How do I map a one-to-many list with String key and no FK

Posted on 2009-05-02
6
641 Views
Last Modified: 2013-11-24
I have 2 views in oracle

parent:
row_id (VARCHAR)
col1
col2

child:
parent_row_id(VARCHAR)
col1
col2(date)

How can do the mapping so that parent has a List containing children
List<Child> children
and which children ordered by col2(date) descending

These 2 view doesn't really have a PK and no FK relationship and child view has no uid but only parent_row_id in (VARCHAR)

Please Advise
Thanks!
0
Comment
Question by:roychan0328
  • 3
  • 3
6 Comments
 
LVL 40

Expert Comment

by:mrjoltcola
ID: 24290053
You can have Hibernate instantiate the relationship for you, or you can instantiate it yourself. It is tempting to do it all in hibernate, but I have found, in my experience, that there are times you have different use cases for loading the same domain object, and in some cases, if don't want dependents loaded, it could hurt performance. I usually manually load child collections and leave Hibernate out of it.

Anyway, to do it with Hibernate, you need to add the bean property to the Parent class:

List<Child> children; // also getters/setters

Then create your parent.hbm.xml as below:

Also create a child.hbm.xml, but if you use inverse="false" you don't have to add anything to the child. If you do want the child to refer back, then you need to declare a relationship in the child hbm file, something like below, but not necessarily with the same otions.

Inside child.hbm.xml

      <many-to-one name="parent" column="PARENT_ROW_ID" class="com.foo.domain.Parent" insert="false" update="false" lazy="false" />

And your Child bean must have "parent" as an exposed bean property. Likewise, Parent needs children as a bean property.

<!-- parent.hbm.xml -->
<hibernate-mapping>
	<class name="com.foo.domain.Parent" table="PARENT">
		<id name="id" column="ROW_ID">
			<generator class="assigned"/>
		</id>
 
		<property name="col1" column="COL1"/>
		<property name="col2" column="COL2"/>
 
		<!-- Associations -->
		<set name="children" inverse="false">
			<key column="PARENT_ROW_ID"/>
			<one-to-many class="com.foo.domain.Child"/>
		</set>       
	</class>
</hibernate-mapping>

Open in new window

0
 

Author Comment

by:roychan0328
ID: 24290093
Thanks mrjoltcola. I totally understand what you mean by not loading the dependents in some cases.

OK. So if I decided not to load the association by hibernate. What is the best way of doing it?

public Collection<Parent> getAllParents(){ ... } this one is easy, just use parent mapping and won;t load dependent

//load dependents
public Parent getParentsById(String key){ ... }
use 1 parent mapping and 1 child mapping and not associated, right? so what is the best approach of doing this? HQL? criteria?


0
 

Author Comment

by:roychan0328
ID: 24290138
And by the way, the child doesn't have a PK. How can I map the id field?

0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 40

Accepted Solution

by:
mrjoltcola earned 500 total points
ID: 24290220
>>//load dependents
>>public Parent getParentsById(String key){ ... }

I think you meant:

public Child getChildByParentId(String parentId) { ... }

I would go the criteria route if you have a single key column, I always do it with Hibernate Dao Support class and criteria objects in the typical case. If you need custom SQL I don't know which to suggest, I am not great with HQL so usually choose Spring JdbcTemplate (or raw JDBC if you don't have Spring), because I like to keep it portable in case I change ORMs (which never happens. :) )

>>And by the way, the child doesn't have a PK. How can I map the id field?

Not good. You must add one or identify a composite key based on the columns already in the child table. is parent_row_id + col1 a candidate key?
If not, you can't use Hibernate if the row is not uniquely identified.

I recommend adding a surrogate primary key (a sequence of GUID), otherwise the Hibernate mapping for a composite key is hairier, and requires a domain object to be created for the key to map to.
0
 

Author Comment

by:roychan0328
ID: 24290239
It is a view, so I guess I can't really do anything on the DB side. But I guess I will just use spring Jdbc template. for the child.

Thanks!

0
 
LVL 40

Expert Comment

by:mrjoltcola
ID: 24290330
Which is a view? The child? Tell your DBA to fix the schema! :)

Good luck.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
spring jars download 1 27
How do I remove an object from a 3 25
what privileges needed for S2 for this function (Oracle 12c)? 3 22
What's wrong with this code? 4 23
Configuring and using Oracle Database Gateway for ODBC Introduction First, a brief summary of what a Database Gateway is.  A Gateway is a set of driver agents and configurations that allow an Oracle database to communicate with other platforms…
How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question