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.  [ 12 posts ] 
Author Message
 Post subject: Using entity-name attribute makes save(Object) impossible ?
PostPosted: Fri Dec 02, 2005 7:33 am 
Regular
Regular

Joined: Tue Oct 26, 2004 3:54 pm
Posts: 60
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 3.0.5

Mapping documents:

Code:
<hibernate-mapping>
  <class name="it.xxx.sce.model.MovimentoStandard" table="MOVIMENTO" entity-name="Movimento">
    <id name="id" type="java.lang.Long" column="ID">
      <generator class="native" />
    </id>
    <property name="azienda" type="java.lang.String" column="AZIENDA" not-null="true" />

[....]


Code between sessionFactory.openSession() and session.close():

Code:
// gives an instance of the required implementation of Movimento
Movimento movimento = myFactory.create();
session.save(movimento);


Full stack trace of any exception that occurs:

Code:
Caused by: org.hibernate.MappingException: Unknown entity: it.xxx.sce.model.MovimentoStandard
        at org.hibernate.impl.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:569)
        at org.hibernate.impl.SessionImpl.getEntityPersister(SessionImpl.java:1086)
        at org.hibernate.engine.ForeignKeys.isTransient(ForeignKeys.java:180)
        at org.hibernate.event.def.AbstractSaveEventListener.getEntityState(AbstractSaveEventListener.java:409)
        at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:82
)
        at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:69)
        at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:468)
        at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:463)
        at it.xxx.framework.beans.HibernateBeanDAO.save(HibernateBeanDAO.java:98)
        ... 25 more


Name and version of the database you are using: DB2/400 UDB

The generated SQL (show_sql=true): None

Debug level Hibernate log excerpt: None


It appears that if I define entity-name for my mapped entities, a query like "from Movimento" works and retrieves instances of MovimentoStandard but session.save(movimento) doesn't work, generating a MappingException.

I saw a method like session.save(entityName, object) but it would be rather difficult to implement this in our framework.

Just want to have confirmation that this is the excepted behaviour (but then I think the query too shouldn't work...).

Thanks in advance,

Giulio


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 02, 2005 7:47 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Well, this is the purpose the the entity-name element. The query works because you use the entity-name; in fact it would work even using the un-qualified class name (MovimentoStandard) due to "auto import" of that name for HQL use.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 02, 2005 7:48 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
submitted too soon ;)

if that's not the behavior you want, then just don't use entity-name.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 02, 2005 7:59 am 
Regular
Regular

Joined: Tue Oct 26, 2004 3:54 pm
Posts: 60
steve wrote:
submitted too soon ;)

if that's not the behavior you want, then just don't use entity-name.


Ok, I see.

Thanks for the reply...

Let me ask you something, then.

Let say we have a standard product, which is customized for every installation and the entities are redefined.

So let say Movimento is an interface which defines how it must be designer, MovimentoStandard is the default implementation and MovimentoCustom is the customized implementation.

Which is the correct pattern for mapping entites which will be subclasses / implemented ?

I tried the entity-name way but it didn't work. If I use inheritance without the interface (Movimento disappears, MovimentoStandard becomes Movimento and MovimentoCustom is a subclass of Movimento), how can I configure the mappings ?

Any link or documentation available ?

Thanks in advance,

Giulio


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 02, 2005 11:40 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
One option is that that would be very similar to :
http://cvs.sourceforge.net/viewcvs.py/hibernate/Hibernate3/test/org/hibernate/test/dynamicentity/

The other option is to use entity-name, like you attempted, but then you need to go "whole hog". Meaning you'd need to define associations using the entity-name and use the overloaded Session methods taking entity-name as a param.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 02, 2005 11:45 am 
Regular
Regular

Joined: Tue Oct 26, 2004 3:54 pm
Posts: 60
steve wrote:
One option is that that would be very similar to :
http://cvs.sourceforge.net/viewcvs.py/hibernate/Hibernate3/test/org/hibernate/test/dynamicentity/

The other option is to use entity-name, like you attempted, but then you need to go "whole hog". Meaning you'd need to define associations using the entity-name and use the overloaded Session methods taking entity-name as a param.


Thanks Steve, really.

Just one "common sense" question... do you think it's worth it ?

I mean, to fully use "entity-name" I should also upgrade to Hibernate 3.1rc3 because the delete(entityName, object) method is missing in 3.0.5 - and I feel like it's a not-preferred approach.

I looked at your testcase for dynamic entity, it's very interesting and somehow more complex than my need.

I think an interesting feature in Hibernate could be a factory based instantiation engine for objects. I don't know if it exists, looking at the docs I didn't find anything like that. What do you think?

Regards,

Giulio


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 02, 2005 11:49 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
You realize those tests are actually two distinct ways to acheive the same thing, right? I don't understand how either of those approaches is anything different or more complex than what you suggest with "instantiation engine".


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 02, 2005 11:59 am 
Regular
Regular

Joined: Tue Oct 26, 2004 3:54 pm
Posts: 60
steve wrote:
You realize those tests are actually two distinct ways to acheive the same thing, right? I don't understand how either of those approaches is anything different or more complex than what you suggest with "instantiation engine".


Yes, I realize we're always talking about entity-name.

The fact is that I'm used to my framework approach of dependency injection and inversion of control, and I tend to think that programming by interfaces is more flexible.

So in my mind I was trying to achieve that with Hibernate : mapping an interface, and letting the developer choose the implementation for each customized installation.

I understand that this is not necessarily possible out-of-the-box, and I'm implementing the entity-name approach in my framework right now.

If the entity-name solution worked in both ways (entity-name -> class, class -> entity-name) my approach would have been succesful; but I see that is not possibile, probably because while the entity-name is unique, you could re-use the same class for more than one entity.

Since customization is my primary concern (well, making it the most cheap possibile, it is), I always use a Factory-based approach.

Regards,

Giulio


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 02, 2005 12:18 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
so explain to me how the Interceptor in http://cvs.sourceforge.net/viewcvs.py/hibernate/Hibernate3/test/org/hibernate/test/dynamicentity/interceptor/ or the Tuplizer in http://cvs.sourceforge.net/viewcvs.py/hibernate/Hibernate3/test/org/hibernate/test/dynamicentity/tuplizer/ is in some way different than a "Factory-based approach"

Quote:
Yes, I realize we're always talking about entity-name.

Great! But that was not my question ;)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 02, 2005 12:31 pm 
Regular
Regular

Joined: Tue Oct 26, 2004 3:54 pm
Posts: 60
steve wrote:
so explain to me how the Interceptor in http://cvs.sourceforge.net/viewcvs.py/hibernate/Hibernate3/test/org/hibernate/test/dynamicentity/interceptor/ or the Tuplizer in http://cvs.sourceforge.net/viewcvs.py/hibernate/Hibernate3/test/org/hibernate/test/dynamicentity/tuplizer/ is in some way different than a "Factory-based approach"

Quote:
Yes, I realize we're always talking about entity-name.

Great! But that was not my question ;)


The result of those approaches are the same, the only difference I see is that a Factory would just read from the configuration which class instantiate for each entity, in a one-to-one relation, while it appears that your testcase has custom code to instantiate one or another class.

I really just need a "replacement" pattern which allows me to address through an entity-name a not known implementation below. The entity-name approach is ok, I now know that to use that approach I must use the overloaded methods which accept the entity-name.

Regards,

Giulio


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 02, 2005 12:52 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
yes, *my code* does do that. *Your* custom Interceptor or *your* custom Tuplizer can do whatever the h3ll it wants! ;)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 02, 2005 12:57 pm 
Regular
Regular

Joined: Tue Oct 26, 2004 3:54 pm
Posts: 60
Now that I know that *my* custom Interceptor(s) and *my* custom Tuplizer(s) can DO HELL, I *truly* recognize the usefulness of your latest post and another credit goes to you!

Actually, thanks for the attention - your help has been precious. My entity-name modification is running and happy!

Giulio


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 12 posts ] 

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.