roychan0328
asked on
Hibernate: How do I map a one-to-many list with String key and no FK
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!
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!
ASKER
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?
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?
ASKER
And by the way, the child doesn't have a PK. How can I map the id field?
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
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!
Thanks!
Which is a view? The child? Tell your DBA to fix the schema! :)
Good luck.
Good luck.
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.Pare
And your Child bean must have "parent" as an exposed bean property. Likewise, Parent needs children as a bean property.
Open in new window