Hibernate mapping of map where key is a part of complex value - java

I have to create complex hibernate mapping. The following simplified example explains my problem.
I have two entities:
public class Work {
private WorkType type;
private Set<Workers>;
...
}
public class Worker {
private Map<WorkType,Work>;
...
}
I have 3 tables:
t_works columns: id, type,...
t_workers columns: id,...
t_work_worker columns: worker_id, work_id.
I want to map the map with hibernate, without copying the type values to t_work_worker.
The problem here is that the map key (WorkType) is a part of the map value (Work).
My hibernate hbm:
<typedef class="org.hibernate.type.EnumType" name="workType">
<param name="enumClass">myPackage.WorkType</param>
<param name="type">12</param>
</typedef>
<class name="work" table="T_WORKS">
<property name="type" type="workType" column="type"/>
<set name="workers" table="T_WORK_WORKER" inverse="true" lazy="false" cascade="none">
<key column="WORK_ID" />
<many-to-many column="WORKER_ID"class="myPackage.Worker"/>
</set>
</class>
<class name="Worker" table="T_WORKERS">
<map name="channels" table="T_WORK_WORKER" lazy="false" cascade="all">
<key column="WORKER_ID" />
<map-key formula="(select w.TYPE from t_works w where w.ID=WORK_ID)"type="workType"/>
<many-to-many column="WORK_ID" class="myPackage.Work"/>
</map>
</class>
This mapping works but requires additional select statement (see formula atribute).
I wonder whether there's a way to map the work type as a key, without using "formula" and without adding the type column to the relations table.

As you said, part of your problem is that you have a circular reference. This is usually not a very good idea, so you might want to think of an alternative structure. Do you really need the circular reference? Why does the worker need to have a map of worktype and work? I would probably make the connection between worker and work in another class, so you won't need the circular reference.

Related

Hibernate transitive collection mapping

I'm working with Hibernate 3.6 version, with xml mapping files. In my case I have three mapped entities, which are Detector, Antenna and Location. Basically, having Detector->Set<Antenna> and Location->Set<Antenna> relations, I would like to have also Detector->Set<Location> available.
Each Detector has a Set of Antenna entities, mapped like that:
<set name="_Antennas" table="tantenna" inverse="true" cascade="all">
<key>
<column name="id_detector" not-null="true" />
</key>
<one-to-many class="Antenna" />
</set>
Also each Antenna belongs to a specific Location and to a specific Detector. That's the many-to-one mapping to refer that:
<many-to-one name="_Detector" class="com.tadic.model.detector.Detector"
column="id_detector" />
<many-to-one name="_Location" class="com.tadic.model.location.Location"
column="id_location" />
In the same way, Location has a Set of its Antennas:
<set name="_Antennas" table="tantenna">
<key>
<column name="id_location" />
</key>
<one-to-many class="com.tadic.model.detector.Antenna" />
</set>
So Detector knows about its Antennas, Antennas know about their Detector and Location. Location entity has a set of its Antennas, but tlocation table has no foreign-keys.
However, I'm interested in knowing all the Locations of a Detector in a specific point. I know I can do it writing an HQL, but I would like to know if this is possible when Detector loads, just mapping it as a Set of Location entities.
Remember tlocation table has no iddetector column to link it with, also I think there's no need for it.
If I got it right from a database point
tdetector [1]--[id_detector]-->[n] tantenna
tlocation [1]--[id_location]-->[m] tantenna
Meaning tantenna has a column tuple of (id_detector, id_location) and is essentially a link table between tdetector and tlocation. This could be used to facilitate a many-to-many mapping between Detectors and Locations.
And here is the mapping fragment for the Detector hibernate mapping.
<set name="locations" table="tantenna">
<key column="id_detector" />
<many-to-many class="com.tadic.model.location.Location" column="id_location" />
</set>
One more thing. In my experience, having such a complex relations scheme mapped on the ORM does not come without cost. Even if hibernate finds your mapping files to be fine during the session factory initialization, I urge you to test thoroughly and, if necessary, specify some relations to be read-only (i.e. only useful when reading data) with insert="false" update="false".

Hibernate: Partial lazy initialization?

I have a many-to-many association defined like:
Parent.hbm.xml:
<set name="children" table="child_parent_map" lazy="true">
<cache usage="nonstrict-read-write" />
<key column="parent_id" />
<many-to-many class="Child">
<column name="child_id" not-null="true" index="child_parent_by_child"/>
</many-to-many>
</set>
Child.hbm.xml:
<set name="parents" table="child_parent_map" lazy="true">
<cache usage="nonstrict-read-write" />
<key column="child_id" />
<many-to-many column="parent_id" class="Parent" lazy="false"/>
</set>
I am quite sure I am initializing Parent.children by walking the collection. Something like:
for(Child child : parent.getChildren()) {
Hibernate.initialize(child.getAnotherProperty());
}
Parent has six children. However, in one session parent appears to have only five, and in another (2 seconds later, nothing changed in DB or in another session) - all six. Actually, I discovered it after detaching these entities from session with a custom cloner.
I thought that lazy collections are either completely initialized (i.e. all elements are), or not. Is it possible that somehow only a part of the collection was initialized? Can it be an issue with caching?
EDIT: This session handles a fairly large data set (a few thousands of entities). Is it possible that this is because some already-loaded entities got evicted from the session?
Start by checking your hashCode() and equals() methods, incorrect implementation of these methods often cause this kind of behavior.

Hibernate, DB2 - Slow running queries

I'm doing a search on one of my tables (legacy database) and recieving a horrible time here. The query is build by criteria api of hibernate, e.g.:
Criteria crit = getSessionFactory().getCurrentSession().createCriteria(P1.class);
crit.add(Restrictions.sqlRestriction("{alias}.compno like ?", "%" + s + "%", new StringType()));
crit.setMaxResults(25);
crit.setFirstResult(0);
crit.addOrder(Order.asc("compno"));
crit.list();
As you can see I'm already doing a paging here to improve the perfomance. This criteria needs ~6 seconds on average.
Well the native query which looks like this
select * from SCHEM.P1 where compno like '%100%' order by compno fetch first 25 rows only
takes only 10 ms which is a huge difference imo. Why does the criteria runs so slow? Need I switch back to the native sql query?
Good point on the comments:
Yes there are some relationships which I didn't had on the scope:
<set name="pI" table="P12" lazy="false">
<key column="awcompno" update="false" />
<one-to-many class="org.gee.hibernate.P12" not-found="ignore"/>
</set>
<one-to-one name="info" class="org.gee.hibernate.P13" />
<set name="ma" table="P03" lazy="true" schema="SCHEMP" mutable="false" >
<key column="macountry" property-ref="land" update="false" />
<one-to-many class="org.gee.hibernate.P03" not-found="ignore" />
</set>
<set name="users" table="P15" lazy="true">
<key column="apcompno" update="false" />
<one-to-many class="org.gee.hibernate.P15" not-found="ignore"/>
</set>
My tip is:
<set name="pI" table="P12" lazy="false">
<key column="awcompno" update="false" />
<one-to-many class="org.gee.hibernate.P12" not-found="ignore"/>
</set>
This collection is not lazy. That may be your bottleneck.
Do you need all informations? You may read fields of your entity with hibernate, if you only want to read the IDs.
IBM pureQuery has some really nice facilities for accelerating Hibernate applications that work with DB2. The other benefit ... it makes it much easier to debug as it allows you to correlate your SQL and your Java code.
Take a look at this article http://www.ibm.com/developerworks/data/library/techarticle/dm-1008hibernateibatispurequery1/index.html
I'd say have a look at the DB logs to check what are the exact SQL instructions that get executed. Hibernate maybe loading more than just the native query you would expect, as it may load eager collections etc.
So - enable Hibernate query logging, or better yet, check the DB logs to see what gets executed.

Hibernate, Set keys, CompositeUserType and SQL Procedures

I am having some trouble with returning a non-empty Set into an object using Hibernate and a custom CompositeUserType key.
I have a set of tables and views (simplified here):
create table lang (lang_id,lang_cd);
create table article (art_id,...);
create table article_lang (art_id, lang_id,title,...);
create view article_lang_vw (select * from article join article_lang on art_id);
create table authors(user_id,...);
create table article_authors(art_id,lang_id,user_id);
And database functions:
create or replace procedure addarticle(title,art_id,lang_id) ...
create or replace procedure updatearticle(title,art_id,lang_id)..
create or replace procedure delarticle(art_id,lang_id)..
create or replace procedure addarticleauthor(user_id,art_id,lang_id)...
create or replace procedure delarticleauthor(user_id,art_id,lang_id)...
So to accomplish this mapping using those functions I had to implement CompositeUserType so now I have Java classes like this:
class ProcedureGenerator implements PostInsertIdentityGenerator ...
class Language { int lang_id }
class ArticleLangPKType implements CompositeUserType { //implemented methods }
class ArticleLangPK { int art_id; Language l; }
class Article { ArticleLangPK id; String title; Set<Author> authors; }
class Author { int user_id; String name; }
I want to have a List or Set of Authors. But cannot figure out how to map this part in the *.hbm.xml files. It currently looks something like this:
<class name="Author" mutable="false">
<id name="user_id"/>
<property name="name"/>
</class>
<class name="Article">
<id name="id" type="ArticleLangPKType">
<column name="art_id"/>
<column name="lang_id"/>
<generator class="ProcedureGenerator"/>
</id>
<property name="title"/>
<set name="authors" table="article_authors">
<key> <!-- <key type="ArticleLangPKType"> -->
<column name="art_id"/>
<column name="lang_id"/>
</key>
<many-to-many class="Author" table="article_authors" unique="true"/>
<!-- addauthor, delauthor sql here some how -->
</set>
<sql-insert callable="true">{call addarticle(?,?,?)}</sql-insert>
<sql-update callable="true">{call updatearticle(?,?,?)}</sql-update>
<sql-delete callable="true">{call adddelete(?,?)}</sql-delete>
</class>
But when I run this session.load(Article.class, pk) on an article I know has authors I get a Set size of zero. Otherwise I have no problems inserting, updating, deleting using Hibernate, but now I am stumped. This seems to me to indicate a problem with my ArticleLangPKType.
Any ideas what to do to complete this? Why is my Set always size 0? How would I save the author using the Article's Set and the SQL procedures as provided? Is Hibernate right for me? Do I need a break to see this clearly?
Thanks in advance!
Nevermind I did need a long break. My ArticleLangPK did not override hashCode and Equals correctly. Now just to figure out how to call those other two stored procedures correctly.

Hibernate Reference column in table to indicate how to unmarshall an attribute in a different column in the same table

I have an entity that I want to persist through Hibernate (3.2)
The EntityBean has a column that indicates how another value of the entity bean should be unmarshalled:
<class name="ServiceAttributeValue" table="service_attribute_value">
<cache usage="nonstrict-read-write"/>
<id name="id" column="id" type="int-long">
<generator class="native"/>
</id>
<property name="serviceAttribute" type="service-attribute" column="service_attribute" not-null="true" />
<!-- order is important here -->
<property name="value" type="attribute-value" not-null="true">
<column name="service_attribute" />
<column name="id_value"/>
<column name="enum_value"/>
<column name="string_value"/>
<column name="int_value"/>
<column name="boolean_value"/>
<column name="double_value"/>
</property>
</class>
The "service_attribute" column indicates which of the columns for the "value" property to look at when it unmarshalls the value and, more importantly, exactly what Type the value should be, for example the class of the Enum if the enum_value is to be read, or the type of Bean if the the id_value is to be read.
The value property uses a custom CompositeUserType to do the unmarshalling and within this I wish to reference the service_attribute column (although not write to it), however when I try to do this I get the following error:
org.hibernate.MappingException: Repeated column in mapping for entity: com.precurse.apps.rank.model.service.ServiceAttributeValue column: service_attribute (should be mapped with insert="false" update="false")
However within the definition of the composite property these xml attributes are not defined (only within a normal property).
Does anyone know of a way of overcoming this, or if there is a better solution to this propblem.
If you need any more information please let me know,
Cheers
Simon
I had a similar problem and changing the case of one column solved the problem. Could give a try!
e.g., one column could be service_attribute other Service_Attribute.
You can try this. Instead of mapping both values as property on the same table, map one of the property using join to itself and keep the other property as the way it is. This case you will be able to access the same property in both places. Just remember to name the property as different name.
<join table="service_attribute_value">
<key column = "id" />
<property name="serviceAttribute" type="service-attribute" column="service_attribute" not-null="true" />
</join>
<!-- order is important here -->
<property name="value" type="attribute-value" not-null="true">
<column name="service_attribute" />
<column name="id_value"/>
<column name="enum_value"/>
<column name="string_value"/>
<column name="int_value"/>
<column name="boolean_value"/>
<column name="double_value"/>
</property>
based on your description, it seems like what you want to do is creating different subclasses based on the service_attribute. Instead of trying to achieve repeated column mapping which is not allow in hibernate, you can take a look hibernate inheritance mapping.
I Think I found a solution albeit not a very elegant one.
in the
public Object nullSafeGet(ResultSet rs, String[] names, SessionImplementor session, Object owner)
throws HibernateException, SQLException {
method of the CompositeUserType the "owner" argument passed to the method contains the id of the object who's service_attribute I want to access.
Annoyingly the actual serviceAttribute of the owner is not accessable or has not been set at this stage (I played around with the ordering of the elements in the hbm.xml config, in case this was an ordering thing, but unfortunatly still no joy), so I can't simply access it.
Anyway the id of the owner object is set, so I then used the session argument to run a HQL query based on the id to access the serviceAttribute which I then used to correctly unmarshall the value property.
The drawback of this solution is that it requires a HQL query as an overhead to the unmarshalling process, although its within the same session, its still not optimal.
If anyone has any ideas for a better solution I'd be very grateful.
Cheers

Categories

Resources