hi,

On Wed 19 Feb 2003 13:14:30 +0100, "Tom Klaasen" <[EMAIL PROTECTED]>
said:
> Following Gavin's recommendations, I've tried to wrap the key in a
> composite-id tag in the .hbm.xml:
> 
> <composite-id name="itemID" class="com.scm.escher.base.ItemID">
>       <key-property name="itemID" column="ITEMID" />
> </composite-id>
> 

if i understand correctly, you have a single field primary key wrapper
class. in this case what i found to be a working approach, is to 

 - write a UserType for your PrimaryKey class
 - write a PK generator, by wrapping some built in generator

eg. in hibernate2: 

public class PrimaryKeyGenerator extends SequenceGenerator {

        public Serializable generate(SessionImplementor session, Object obj) throws 
SQLException, HibernateException {
                Long id = (Long)super.generate(session, obj);
                return new PrimaryKey(id.toString());
        }


        public void configure(Type type, Properties props, Dialect dialect) throws 
MappingException {
                if (PrimaryKey.class != type.getReturnedClass()) {
                        throw new MappingException("This generator doesn't support PKs 
of type "+type.getReturnedClass());
                }
                super.configure(Hibernate.LONG, props, dialect);
        }

}

then your mapping could look like
        <id name="PK" type="foo.bar.PrimaryKeyType">
                <column name="id" length="16"/>
                <generator class="foo.bar.PrimaryKeyGenerator">
                        <param name="sequence">MYSEQ</param>
                </generator>
        </id>


this way hibernate can still automatically generate your PKs... now in
your case it seems you have one PK class per entity, so here's what i
think you need to do in addition to what i described above:

 - modify PrimaryKeyGenerator above, so it dynamically instantiates the
 PK class (eg. use type.getReturnedClass().newInstance())
 - for each PK class, extend your UserType and override "public Class
 returnedClass()" so it returns the right type.. you could make this an
 public static inner class, something like this:

public class BasePK { 
    ....
    public static class PKType implements UserType {
       ....
       // bulk of UserType here
    }
}

public class UserPK extends BasePK {

 public static PKType extends BasePK.PKType {
       public Class returnedClass() { 
         return UserPK.class;
       }
 }

}

so, finallly you end up with an extra one-line method in all your pk
classes, and this in the mapping file:

        <id name="PK" type="foo.bar.UserPK$PKType">
                <column name="id" length="16"/>
                <generator class="foo.bar.PrimaryKeyGenerator">
                        <param name="pkclass">foo.bar.UserPK</param>
                        <param name="sequence">MYSEQ</param>
                </generator>
        </id>

but this way you can have automatic key generation for custom PK types so
you dont have to worry about manually assigning IDs...

if someone found this useful, i'll add this to the hibernate Wiki :)

best regards,
     viktor



-- 
http://www.fastmail.fm - Access your email from home and the web


-------------------------------------------------------
This SF.net email is sponsored by: SlickEdit Inc. Develop an edge.
The most comprehensive and flexible code editor you can use.
Code faster. C/C++, C#, Java, HTML, XML, many more. FREE 30-Day Trial.
www.slickedit.com/sourceforge
_______________________________________________
hibernate-devel mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/hibernate-devel


Reply via email to