From: <phr...@im...> - 2002-12-09 16:25:07
|
hi, On 08 Dec 2002 10:25:30 -0500, "Chris Winters" <cwi...@op...> said: > On Sat, 2002-12-07 at 19:12, phr...@im... wrote: > > how can one use an alternative class for primary keys (eg. MyPrimaryKey > > instead of a string)? I tried using a UserType for this purpose, but > > reflection fails with an IllegalArgumentException in > > AbstractEntityPersiser.setIdentifier. > > Lookup the 'composite-id' property in the docs. Here's an example for > one of my objects: i considered this approach (even though my pk class is not a composite). however, the problem is that i can no longer rely on hibernate to generate the PKs for me. that's why i was trying to use a UserType. so, is there a way to reuse the functionality in the hibernate PK generators when using a composite-id? thanks, viktor -- phr...@im... -- http://fastmail.fm - IMAP accessible web-mail |
From: Gavin K. <Gav...@ex...> - 2002-12-09 22:10:16
|
This has been requested once before, and it would be very=20 trivial to add support for a <generator> subelement of <composite-id>. But theres always something very fishy about using a generated (ie. synthetic) composite id. It really makes no sense. It never even makes sense to have a single <key-property> be generated.... I can't imagine why you would want it.... > i considered this approach (even though my pk class is not a=20 > composite). however, the problem is that i can no longer rely=20 > on hibernate to generate the PKs for me. that's why i was=20 > trying to use a UserType. so, is there a way to reuse the=20 > functionality in the hibernate PK generators when using a=20 > composite-id? ********** CAUTION - Disclaimer ********** This message may contain privileged and confidential information. If you are not the intended recipient of this message (or responsible for delivery of the message to such person) you are hereby notified that any use, dissemination, distribution or reproduction of this message is prohibited. If you have received this message in error, you should destroy it and kindly notify the sender by reply e-mail. Please advise immediately if you or your employer do not consent to Internet e-mail for messages of this kind. Opinions, conclusions and other information in this message that do not relate to the official business of Expert Information Services Pty Ltd ("The Company") shall be understood as neither given nor endorsed by it. The Company advises that this e-mail and any attached files should be scanned to detect viruses. The Company accepts no liability for loss or damage (whether caused by negligence or not) resulting from the use of any attached files. **EIS******** End of Disclaimer ********** |
From: <phr...@im...> - 2002-12-09 23:16:21
|
hi, On Tue, 10 Dec 2002 09:10:09 +1100, "Gavin King" <Gav...@ex...> said: > This has been requested once before, and it would be very > trivial to add support for a <generator> subelement of > <composite-id>. But theres always something very fishy > about using a generated (ie. synthetic) composite id. It > really makes no sense. It never even makes sense to have a > single <key-property> be generated.... > > I can't imagine why you would want it.... i'm trying to port some legacy code over to hibernate. the code relies on a PrimaryKey class for identifying objects. this is not a composite key, so automatic generation totally makes sense (it really just encapsulates a String id property). changing this all over the place would be somewhat painful, that's why i was trying to work around it within the boundaries of Hibernate. i would like to know what sort of direction would you recommend, eg - replace the PrimaryKey class with String everywhere (-> pain) - extend hibernate along the lines you mentioned (sounds good to me, but i need further directions) - can i just directly use the guts of hibernate to get a new ID? - map it some other way (i tried a UserType, didn't really work out) thanks, viktor -- phr...@im... -- http://fastmail.fm - Faster than the air-speed velocity of an unladen european swallow |
From: Gavin K. <Gav...@ex...> - 2002-12-09 23:34:49
|
Oh, so it isn't actually a composite key. I didn't understand properly then. Well, using a UserType is exactly the right=20 approach. You would also need to implement IdentifierGenerator to generate instances of PrimaryKey (your implementation could=20 just wrap up a built-in key generation strategy). This should work out perfectly - if you have any more problems let me know. The mapping would be: <id name=3D"key" type=3D"my.pkg.PrimaryKeyType"> <generator class=3D"my.pkg.PrimaryKeyGenerator"/> </id> > -----Original Message----- > From: phr...@im... [mailto:phr...@im...]=20 > Sent: Tuesday, 10 December 2002 10:16 AM > To: Gavin King; Chris Winters > Cc: hib...@li... > Subject: RE: [Hibernate] custom identifier class >=20 >=20 > hi, >=20 > On Tue, 10 Dec 2002 09:10:09 +1100, "Gavin King"=20 > <Gav...@ex...> > said: > > This has been requested once before, and it would be very > > trivial to add support for a <generator> subelement of > > <composite-id>. But theres always something very fishy > > about using a generated (ie. synthetic) composite id. It > > really makes no sense. It never even makes sense to have a > > single <key-property> be generated.... > >=20 > > I can't imagine why you would want it.... >=20 > i'm trying to port some legacy code over to hibernate. the=20 > code relies on a PrimaryKey class for identifying objects.=20 > this is not a composite key, so automatic generation totally=20 > makes sense (it really just encapsulates a String id=20 > property). changing this all over the place would be somewhat=20 > painful, that's why i was trying to work around it within the=20 > boundaries of Hibernate. >=20 > i would like to know what sort of direction would you recommend, eg > - replace the PrimaryKey class with String everywhere (-> pain) > - extend hibernate along the lines you mentioned (sounds=20 > good to me, but i need further directions) > - can i just directly use the guts of hibernate to get a new ID? > - map it some other way (i tried a UserType, didn't really work out) >=20 > thanks, > viktor > --=20 > =20 > phr...@im... >=20 > --=20 > http://fastmail.fm - Faster than the air-speed velocity of an > unladen european swallow >=20 ********** CAUTION - Disclaimer ********** This message may contain privileged and confidential information. If you are not the intended recipient of this message (or responsible for delivery of the message to such person) you are hereby notified that any use, dissemination, distribution or reproduction of this message is prohibited. If you have received this message in error, you should destroy it and kindly notify the sender by reply e-mail. Please advise immediately if you or your employer do not consent to Internet e-mail for messages of this kind. Opinions, conclusions and other information in this message that do not relate to the official business of Expert Information Services Pty Ltd ("The Company") shall be understood as neither given nor endorsed by it. The Company advises that this e-mail and any attached files should be scanned to detect viruses. The Company accepts no liability for loss or damage (whether caused by negligence or not) resulting from the use of any attached files. **EIS******** End of Disclaimer ********** |
From: <phr...@im...> - 2002-12-10 17:35:00
|
hi, On Tue, 10 Dec 2002 10:34:30 +1100, "Gavin King" <Gav...@ex...> said: > Oh, so it isn't actually a composite key. I didn't understand > properly then. Well, using a UserType is exactly the right > approach. You would also need to implement IdentifierGenerator > to generate instances of PrimaryKey (your implementation could > just wrap up a built-in key generation strategy). This should > work out perfectly - if you have any more problems let me know. > > The mapping would be: > > <id name="key" type="my.pkg.PrimaryKeyType"> > <generator class="my.pkg.PrimaryKeyGenerator"/> > </id> i created my own PK generator, but i'm still getting the same exception i had before: cirrus.hibernate.PropertyAccessException: IllegalArgumentException occurred while calling setter of test.hibernate.Foo.pk at cirrus.hibernate.helpers.ReflectHelper.set(ReflectHelper.java:190) at cirrus.hibernate.persister.AbstractEntityPersister.setIdentifier(AbstractEntityPersister.java:281) at cirrus.hibernate.impl.SessionImpl.doSave(SessionImpl.java:528) at cirrus.hibernate.impl.SessionImpl.save(SessionImpl.java:483) at cirrus.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:919) at test.hibernate.ejb.HibTestSessionBean.saveFoo(HibTestSessionBean.java:53) from Foo.hbm.xml: <id type="test.hibernate.MyPrimaryKeyType" name="pk" column="id" length="16" unsaved-value="null"> <generator class="test.hibernate.MySequencePKGenerator"> <param>MY_SEQ</param> </generator> </id> Foo.java has: public PrimaryKey getPk() public void setPk(PrimaryKey pk) (i tried adding setPk(Long), setPk(String), setPK(long) - that didnt chg the exception at all) My pk generator extends SequenceGenerator, and overrides one method as follows: public Serializable generate(SessionImplementor session, Object obj) throws SQLException, HibernateException { return new PrimaryKey( String.valueOf(super.generate(session, obj)) ); } the implementation of my UserType is as follows: private static final int[] TYPES = { Types.VARCHAR }; public int[] sqlTypes() { return TYPES; } public Class returnedClass() { return PrimaryKey.class; } public boolean equals(Object x, Object y) { if (x==y) return true; if (x==null || y==null) return false; return x.equals(y); } public Object nullSafeGet(ResultSet rs, String[] names, Object owner) throws HibernateException, SQLException { String id = (String) Hibernate.STRING.nullSafeGet(rs, names[0]); return id==null ? null : new PrimaryKey(id); } public void nullSafeSet(PreparedStatement st, Object value, int index) throws HibernateException, SQLException { String id = (value==null) ? null : ((PrimaryKey)value).getId(); Hibernate.STRING.nullSafeSet(st, id, index); } public Object deepCopy(Object o) { if (o==null) return null; return new PrimaryKey( (PrimaryKey)o ); } public boolean isMutable() { return false; } any help would be appreciated :) viktor -- phr...@im... -- http://fastmail.fm - Does exactly what it says on the tin |
From: <phr...@im...> - 2002-12-16 20:14:05
|
hi, excuse my arrogance for re-posting this, but i haven't found a solution for the problem described below: On Tue, 10 Dec 2002 12:34:56 -0500, phr...@im... said: > hi, > > On Tue, 10 Dec 2002 10:34:30 +1100, "Gavin King" <Gav...@ex...> > said: > > Oh, so it isn't actually a composite key. I didn't understand > > properly then. Well, using a UserType is exactly the right > > approach. You would also need to implement IdentifierGenerator > > to generate instances of PrimaryKey (your implementation could > > just wrap up a built-in key generation strategy). This should > > work out perfectly - if you have any more problems let me know. > > > > The mapping would be: > > > > <id name="key" type="my.pkg.PrimaryKeyType"> > > <generator class="my.pkg.PrimaryKeyGenerator"/> > > </id> > > i created my own PK generator, but i'm still getting the same exception i > had before: > > cirrus.hibernate.PropertyAccessException: IllegalArgumentException > occurred while calling setter of test.hibernate.Foo.pk > at cirrus.hibernate.helpers.ReflectHelper.set(ReflectHelper.java:190) > at > cirrus.hibernate.persister.AbstractEntityPersister.setIdentifier(AbstractEntityPersister.java:281) > at cirrus.hibernate.impl.SessionImpl.doSave(SessionImpl.java:528) > at cirrus.hibernate.impl.SessionImpl.save(SessionImpl.java:483) > at cirrus.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:919) > at > test.hibernate.ejb.HibTestSessionBean.saveFoo(HibTestSessionBean.java:53) > > from Foo.hbm.xml: > > <id type="test.hibernate.MyPrimaryKeyType" name="pk" column="id" > length="16" unsaved-value="null"> > <generator class="test.hibernate.MySequencePKGenerator"> > <param>MY_SEQ</param> > </generator> > </id> > > Foo.java has: > > public PrimaryKey getPk() > public void setPk(PrimaryKey pk) > (i tried adding setPk(Long), setPk(String), setPK(long) - that didnt > chg the exception at all) > > > My pk generator extends SequenceGenerator, and overrides one method as > follows: > > public Serializable generate(SessionImplementor session, Object obj) > throws SQLException, HibernateException { > return new PrimaryKey( String.valueOf(super.generate(session, obj)) ); > } > > > the implementation of my UserType is as follows: > > private static final int[] TYPES = { Types.VARCHAR }; > > public int[] sqlTypes() { > return TYPES; > } > > public Class returnedClass() { > return PrimaryKey.class; > } > > public boolean equals(Object x, Object y) { > if (x==y) return true; > if (x==null || y==null) return false; > return x.equals(y); > } > > public Object nullSafeGet(ResultSet rs, String[] names, Object owner) > throws HibernateException, SQLException { > String id = (String) Hibernate.STRING.nullSafeGet(rs, names[0]); > return id==null ? null : new PrimaryKey(id); > } > > public void nullSafeSet(PreparedStatement st, Object value, int index) > throws HibernateException, SQLException { > String id = (value==null) ? null : ((PrimaryKey)value).getId(); > Hibernate.STRING.nullSafeSet(st, id, index); > } > > public Object deepCopy(Object o) { > if (o==null) return null; > return new PrimaryKey( (PrimaryKey)o ); > } > > public boolean isMutable() { > return false; > } > > > any help would be appreciated :) > > viktor > -- > > phr...@im... > > -- > http://fastmail.fm - Does exactly what it says on the tin -- phr...@im... -- http://fastmail.fm - Does exactly what it says on the tin |
From: Gavin K. <Gav...@ex...> - 2002-12-16 22:24:35
|
Why don't you try stepping through code. Put a break point in=20 AbstractEntityPersister.setIdentifier() and see exactly what class of object is passed in. (This should definately all work as advertised.) > -----Original Message----- > From: phr...@im... [mailto:phr...@im...]=20 > Sent: Tuesday, 17 December 2002 7:14 AM > To: hib...@li... > Subject: RE: [Hibernate] custom identifier class >=20 >=20 > hi, >=20 > excuse my arrogance for re-posting this, but i haven't found=20 > a solution for the problem described below: >=20 > On Tue, 10 Dec 2002 12:34:56 -0500, phr...@im... said: > > hi, > >=20 > > On Tue, 10 Dec 2002 10:34:30 +1100, "Gavin King"=20 > > <Gav...@ex...> > > said: > > > Oh, so it isn't actually a composite key. I didn't understand=20 > > > properly then. Well, using a UserType is exactly the=20 > right approach.=20 > > > You would also need to implement IdentifierGenerator to generate=20 > > > instances of PrimaryKey (your implementation could just wrap up a=20 > > > built-in key generation strategy). This should work out=20 > perfectly -=20 > > > if you have any more problems let me know. > > >=20 > > > The mapping would be: > > >=20 > > > <id name=3D"key" type=3D"my.pkg.PrimaryKeyType"> > > > <generator class=3D"my.pkg.PrimaryKeyGenerator"/> > > > </id> > >=20 > > i created my own PK generator, but i'm still getting the same=20 > > exception i had before: > >=20 > > cirrus.hibernate.PropertyAccessException: IllegalArgumentException=20 > > occurred while calling setter of test.hibernate.Foo.pk at=20 > > cirrus.hibernate.helpers.ReflectHelper.set(ReflectHelper.java:190) > > at > >=20 > cirrus.hibernate.persister.AbstractEntityPersister.setIdentifier(Abstr > > actEntityPersister.java:281) > > at cirrus.hibernate.impl.SessionImpl.doSave(SessionImpl.java:528) > > at cirrus.hibernate.impl.SessionImpl.save(SessionImpl.java:483) > > at=20 > cirrus.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:919) > > at > >=20 > test.hibernate.ejb.HibTestSessionBean.saveFoo(HibTestSessionBe > an.java:53) > >=20 > > from Foo.hbm.xml: > >=20 > > <id type=3D"test.hibernate.MyPrimaryKeyType" name=3D"pk" column=3D"id"= > > length=3D"16" unsaved-value=3D"null"> > > <generator class=3D"test.hibernate.MySequencePKGenerator"> > > <param>MY_SEQ</param> > > </generator> > > </id> > >=20 > > Foo.java has: > >=20 > > public PrimaryKey getPk() > > public void setPk(PrimaryKey pk) > > (i tried adding setPk(Long), setPk(String), setPK(long) -=20 > that didnt > > chg the exception at all) > >=20 > >=20 > > My pk generator extends SequenceGenerator, and overrides=20 > one method as > > follows: > >=20 > > public Serializable generate(SessionImplementor session,=20 > Object obj) > > throws SQLException, HibernateException { > > return new PrimaryKey(=20 > String.valueOf(super.generate(session, obj)) ); > > } > >=20 > >=20 > > the implementation of my UserType is as follows: > >=20 > > private static final int[] TYPES =3D { Types.VARCHAR }; > >=20 > > public int[] sqlTypes() { > > return TYPES;=20 > > } > >=20 > > public Class returnedClass() { > > return PrimaryKey.class; > > } > >=20 > > public boolean equals(Object x, Object y) { > > if (x=3D=3Dy) return true; > > if (x=3D=3Dnull || y=3D=3Dnull) return false; > > return x.equals(y); > > } > >=20 > > public Object nullSafeGet(ResultSet rs, String[] names,=20 > Object owner) > > throws HibernateException, SQLException { > > String id =3D (String) Hibernate.STRING.nullSafeGet(rs, names[0]); > > return id=3D=3Dnull ? null : new PrimaryKey(id); > > } > >=20 > > public void nullSafeSet(PreparedStatement st, Object=20 > value, int index) > > throws HibernateException, SQLException { > > String id =3D (value=3D=3Dnull) ? null : ((PrimaryKey)value).getId(); > > Hibernate.STRING.nullSafeSet(st, id, index); > > } > >=20 > > public Object deepCopy(Object o) { > > if (o=3D=3Dnull) return null; > > return new PrimaryKey( (PrimaryKey)o ); > > } > >=20 > > public boolean isMutable() { > > return false; > > } > >=20 > >=20 > > any help would be appreciated :) > >=20 > > viktor > > -- > > =20 > > phr...@im... > >=20 > > -- > > http://fastmail.fm - Does exactly what it says on the tin > --=20 > =20 > phr...@im... >=20 > --=20 > http://fastmail.fm - Does exactly what it says on the tin >=20 >=20 > ------------------------------------------------------- > This sf.net email is sponsored by: > With Great Power, Comes Great Responsibility=20 > Learn to use your power at OSDN's High Performance Computing=20 > Channel http://hpc.devchannel.org/=20 > _______________________________________________ > hibernate-devel mailing list hib...@li... > https://lists.sourceforge.net/lists/listinfo/hibernate-devel >=20 ********** CAUTION - Disclaimer ********** This message may contain privileged and confidential information. If you are not the intended recipient of this message (or responsible for delivery of the message to such person) you are hereby notified that any use, dissemination, distribution or reproduction of this message is prohibited. If you have received this message in error, you should destroy it and kindly notify the sender by reply e-mail. Please advise immediately if you or your employer do not consent to Internet e-mail for messages of this kind. Opinions, conclusions and other information in this message that do not relate to the official business of Expert Information Services Pty Ltd ("The Company") shall be understood as neither given nor endorsed by it. The Company advises that this e-mail and any attached files should be scanned to detect viruses. The Company accepts no liability for loss or damage (whether caused by negligence or not) resulting from the use of any attached files. **EIS******** End of Disclaimer ********** |
From: <phr...@im...> - 2002-12-18 18:24:08
|
hi, it seems to be some sort of class-loading issue - the PrimaryKey class that the generator returns, and the one that the bean uses seems to have been loaded by a different classloader, so even though they are the same class, they are not compatible. have someone encountered the same issue? is there a way i can make hibernate use the same classloader? best regards, viktor On Tue, 17 Dec 2002 09:24:23 +1100, "Gavin King" <Gav...@ex...> said: > Why don't you try stepping through code. Put a break point in > > AbstractEntityPersister.setIdentifier() > > and see exactly what class of object is passed in. (This should > definately all work as advertised.) > > > > > > > cirrus.hibernate.PropertyAccessException: IllegalArgumentException > > > occurred while calling setter of test.hibernate.Foo.pk at > > > cirrus.hibernate.helpers.ReflectHelper.set(ReflectHelper.java:190) > > > at > > > > > cirrus.hibernate.persister.AbstractEntityPersister.setIdentifier(Abstr > > > actEntityPersister.java:281) > > > at cirrus.hibernate.impl.SessionImpl.doSave(SessionImpl.java:528) > > > at cirrus.hibernate.impl.SessionImpl.save(SessionImpl.java:483) > > > at > > cirrus.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:919) > > > at > > > > > test.hibernate.ejb.HibTestSessionBean.saveFoo(HibTestSessionBe > > an.java:53) > > > > -- phr...@im... -- http://fastmail.fm - Consolidate POP email and Hotmail in one place |