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.  [ 22 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Hibernate Tools, Sets & Keys
PostPosted: Wed Jan 25, 2006 12:24 pm 
Beginner
Beginner

Joined: Mon Jun 06, 2005 6:48 pm
Posts: 45
Hi,

I am using Hibernate tools to generate the Vos to be used with Hibernate and am noticing something odd that perhaps has a simple answer.

When using for example two tables and the reverse engineering into java VO's (Value Objects) I end up with two objects which one object is declared as a set inside the other. If I set the values in the set which is a primary key and also set the primary keys/values of the parent of the set or the first object I get the first object saved into the database but the set which is contained/declared inside the first VO does not get saved. I am using the add method for a to store the objects for the second table in the set.

My question is should I be using some sort of key when using the add method so the hibernate tools mapping files will work? Or does the Hibernate Tools file mapping assume you to create some sort of key to be used when adding objects to a set and if so what is it looking for?

I am hoping that the Mapping that Hibernate tools generates does not require a lot of work to use such that if I fill in the required fields (database not null columns) in a Hibernate genereated VO for a set that no other coding is required.

Thanks,
-Tony


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 25, 2006 12:30 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
the values in the set should *not* be primary keys, but instances of the referenced objects.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 25, 2006 1:18 pm 
Beginner
Beginner

Joined: Mon Jun 06, 2005 6:48 pm
Posts: 45
Hi Max,

The objects in the set are VO's created by Hibernate Tools and in those objects are some attributes considered keys in the database.

So if you have an hibenrate tools generated vo named autoMaker and it has an attribute which is a set which contains car objects and the car object has a attribute (not nullable) called serialNumber and one called model the serialNumber is a non-wrapper long class which is mapped to a column in the car table of an object.

Sorry my explanation may have simpliefied things too much and caused some misinterpretation.

Does that help?
-Tony

P.S. I was able to a build form CVS to work so I have a working hibernate tools jar that creates both long and Long correctly based on nullable fields.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 25, 2006 1:45 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
i still don't understand your problem.

Can you show it in code ? ;)

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 26, 2006 12:28 pm 
Beginner
Beginner

Joined: Mon Jun 06, 2005 6:48 pm
Posts: 45
Hi Max,

I had something I had to deal with so am finally back today.

For example we have an AutoMaker class generated by Hibernate Tools

public class AutoMaker {

private Set Car cars;

AutoMaker() {}

public void setCars(Car vo) {
car = vo;
}

public Car getCars() {
return car;
}

}

public class Car {

private long serialNumber;
private String color;

public Car() {}

public setSerialNumber(long id) {
serialNumber = id;
}

public long getSerialNumber() {
return serialNumber;
}

public void setColor(String val) {
color = val;
}

public String getColor() {
return color;
}
}


So. I have several questions:

1. Assuming Hibernate Tools maps the serialNumber value to the primary column in the table Car does Hibernate Tools tie the Car instance such that if I populate the serialNumber attribute in an instance of Car and add that to an instance of HashSet and that instance is passed to the setCar in an instance the AutoMaker class that an entry in the Car table will be inserted or updated when an instance of AutoMaker is passed to a Hibernate seesion and committed?

so:

AutoMaker autoMaker = new AutoMaker();
Car car = new Car();
car.setSerialNumber(10);
HashSet cars = new HashSet();
cars.add(car);
autoMaker.setCars(cars);

session.saveOrUpdate(cars);
session.flush();
session.connection.commit();


So this should work because of the mapping generated by the tools or is there some key I need to generate for the set so when I add the cars to the HashSet I provide some sort of key in addition to the car object?


Thanks,
-Tony


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 26, 2006 1:18 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
the reverse engineering does not do any cascading (for now)

As it says in the beginning of the docs, you should not think that using the tools allows you to not understand how hibernate works ;)

So go read the docs of hibernate and let it help you understand what is atually generated.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 26, 2006 1:34 pm 
Beginner
Beginner

Joined: Mon Jun 06, 2005 6:48 pm
Posts: 45
Hi Max,

I may be wrong but are you saying all those wonderfull hibernate mapping files are not used beyond the first level? So for the example I listed I have to do a saveOrUpdate for each individual table?
So I would:

1 save Car
2 then save AutoMaker?

I hope I am wrong in my assumption.

-Tony


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 26, 2006 1:56 pm 
Beginner
Beginner

Joined: Mon Jun 06, 2005 6:48 pm
Posts: 45
Hi Max & All,

If I am correct in my previous assumption regarding cascading then:

1. When will cascading be implemented?
2. Will Middlegen solve this issue?

I think the tools group is doing a great job but I need to be practical.

Thanks,
-Tony


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 26, 2006 2:21 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
1. as soon as possible ...or you could just update the *.hbm.vm to set the cascade or default-cascade to where you want it. The problem is how I can know which associations/collections you want cascades to occur ?

2. they have added some control over this yes.

3. put feature requests into jira.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 26, 2006 3:08 pm 
Beginner
Beginner

Joined: Mon Jun 06, 2005 6:48 pm
Posts: 45
Hi Max,

I understand exactly what you are talking about. For example the user may want the cascades not to ripple into the tables that are used for lookups such as a country or zip code table. One way for the user to control this issue would be to use the reverse engineering file to exclude those tables you never want to insert/update or delete. Then you can put the enhancement in to add the needed tags into the mapping files.

Perhaps you might have some other use cases I have not considered yet?

The mapping files created by the tool look as if all the tables I requested by reverse engineering are there. So if I add a cascade=true to the files I want cascading on I should hopefully be set?

Can I set this tag in the reverse engineering config file and if so how do I do that?

Thanks,
-Tony


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 26, 2006 3:18 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
you understand what the issue is, but i think you still need to read how cascading works in hibernate ;)

there is no cascade=true option. A cascade can be a list of various operations, such as "save-update", "merge", etc.

You can go in and change the set.hbm.vm to set the cascade attirbute to what you want.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 26, 2006 3:57 pm 
Beginner
Beginner

Joined: Mon Jun 06, 2005 6:48 pm
Posts: 45
Hi Max,

Thanks for the answer. I will read the hibernate docs and look at the velocity template and try to get the tag included.

I will let you know how it works out.

-Tony


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 26, 2006 5:31 pm 
Beginner
Beginner

Joined: Mon Jun 06, 2005 6:48 pm
Posts: 45
Hi Max,

I ran into an interesting issue that you might want to take into account when adding the enhancement to the tools for cascade.

I used the reverse engineering tool to exclude the lookup tables and I ran into something I expected would happen. That is the tools still of course created the relationship back to the lookups from tables that have relationships back to those llokups and at runtime you get the error message from hibernate about no mapping file found for the lookup that another table was found to have a relationship back to.

So, if you have a table such as employee have a relationship back to country and the country lookup is removed then you get a runtime exception about the employee mapping back to country but no country mapping file found.

I am sure you have an answer about how to keep hibernate happy without writing back to the country table.

Thought you might be interested to know this.

-Tony


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 26, 2006 6:02 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
huh ? are you sure that you excluded the table ? if the tools are generating relationships to excluded tables it is a bug!

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject: default-cascade specification in ant tools reverse engineer?
PostPosted: Wed Aug 16, 2006 11:36 am 
Newbie

Joined: Mon Oct 17, 2005 11:56 am
Posts: 16
Location: Arlington, VA
Hi All,

I would like to be able to define my default-cascade options for specific relationships from my hibernate.reveng.xml file.

Has anyone else requested this? Is it implemented?

The alternative suggested earlier - changing .vm templates to generate default-cascade="save-update" would apply to every single relationship. Also, tools is using .ftl templates now.

I see in hibernate-mapping.hbm.ftl, that it checks for "hmgs.hasNonDefaultSettings()" - how can I set this up with hmgs.defaultCascade="all"?

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<!-- Generated ${date} by Hibernate Tools ${version} -->
<#if hmgs?exists && hmgs.hasNonDefaultSettings()>
<hibernate-mapping
<#if hmgs.hasDefaultPackage()>
package="${hmgs.defaultPackage}"
</#if>
<#if hmgs.hasSchemaName()>
schema="${hmgs.schemaName}"
</#if>
<#if hmgs.hasCatalogName()>
catalog="${hmgs.catalogName}"
</#if>
<#if hmgs.hasNonDefaultCascade()>
default-cascade="${hmgs.defaultCascade}"
</#if>

<#if hmgs.hasNonDefaultAccess()>
default-access="${hmgs.defaultAccess}"
</#if>
<#if !hmgs.isDefaultLazy()>
default-lazy="false"
</#if>
<#if !hmgs.isAutoImport()>
auto-import="false"
</#if>>
<#else>
<hibernate-mapping>
</#if>

<#include "persistentclass.hbm.ftl"/>

</hibernate-mapping>

Thanks for any advice!
Vick

Versions:
Java 5
EJB 2
Hibernate 3.0.5
Hibernate Tools 3.1.0-beta5


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 22 posts ]  Go to page 1, 2  Next

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.