views:

102

answers:

1

Hi, 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!

A: 

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.

Steve