-->
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.  [ 6 posts ] 
Author Message
 Post subject: Checking if entity exist
PostPosted: Fri May 27, 2005 3:47 am 
Newbie

Joined: Thu May 12, 2005 1:31 pm
Posts: 4
Is it possible to check if given entity (class mapped with some 'entity-name') exist? I wasn't able to find method for this task.

M.P.


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 27, 2005 4:34 am 
Senior
Senior

Joined: Sun Mar 14, 2004 10:16 am
Posts: 129
Location: Ankara
query it!

_________________
-developer


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 27, 2005 7:14 am 
Expert
Expert

Joined: Sat Oct 25, 2003 8:49 am
Posts: 490
Location: Vrhnika, Slovenia
boolean exists = session.get(clazz, id) != null;


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 27, 2005 4:57 pm 
Newbie

Joined: Thu May 12, 2005 1:31 pm
Posts: 4
Query it? How?

session.createQuery("from SomeEntity").list() throws exception, if SomeEntity does not exist. I don't like this way.

I can't use session.get() either, because I can't imply that particular id is valid.

Entity name is passed to servlet as a request parameter. Servlet calls Dao layer and obtain list of objects from table mapped to this entity. But if entity don't exist, exception is throwed...


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 27, 2005 10:26 pm 
Beginner
Beginner

Joined: Thu Feb 17, 2005 4:20 pm
Posts: 29
does this do it?

Code:
SessionImplementor session = (SessionImplementor) HibernateSessionManager.getSession();
final PersistenceContext persistenceContext = session.getPersistenceContext();
EntityKey key = new EntityKey( entity.getId(), session.getEntityPersister( null, entity) , session.getEntityMode()  );

if ( persistenceContext.containsEntity( key ) ) {
  //entity exits
} else {
  //entity does not exist
}


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 31, 2005 3:05 am 
Newbie

Joined: Thu May 12, 2005 1:31 pm
Posts: 4
uhm.. no ;)

You are talking about entity = loaded from database object. I'm thinking about class mapped with "entity-name" property (Hibernate 3.x feature).

Anyway, I find it:

Session session = ...
boolean exist = session.getSessionFactory().getClassMetadata("some_mapped_entity") != null;


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.