-->
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.  [ 11 posts ] 
Author Message
 Post subject: Get FK y PK from hibernate API
PostPosted: Wed Mar 10, 2010 7:28 am 
Newbie

Joined: Wed Mar 10, 2010 7:16 am
Posts: 7
Hi, I am displaying hibernate entities and I would like to get Relations from JDBC or XBM.XML files. I am sure It could be done from de API but I do not find out.

What I am looking for is equivalents for

java.sql.DatabaseMetaData
ResultSet getPrimaryKeys(String catalog, String schema, String table)
ResultSet getImportedKeys(String catalog, String schema, String table)
ResultSet getExportedKeys(String catalog, String schema, String table)

I would like to get this because I have to display data entities and its relations with other entities.

thanks,
txemi


Top
 Profile  
 
 Post subject: Re: Get FK y PK from hibernate API
PostPosted: Wed Mar 10, 2010 9:20 am 
Regular
Regular

Joined: Thu May 07, 2009 5:56 am
Posts: 94
Location: Toulouse, France
I think you can try org.hibernate.cfg.Configuration.getTableMappings(). this method provides you away to get org.hibernate.mapping.Table instances. Table class provides: getPrimaryKey(), getForeignKeyIterator() etc

_________________
everything should be made as simple as possible, but not simpler (AE)


Top
 Profile  
 
 Post subject: Re: Get FK y PK from hibernate API
PostPosted: Wed Mar 10, 2010 10:33 am 
Newbie

Joined: Wed Mar 10, 2010 7:16 am
Posts: 7
Thanks, that is exactly what I was looking for.
I played with it.
Now I am trying to reach this information from a session object. The reason is that most projects provides a singleton sessionfactory but not to the configuration object. If I achieved this I would not have to modify this. ¿Could I get this mapping or Configuration object from session or sessionFactory? I am looking for it but I do not success.
thanks

Code:
public static void main(String[] args) {
      Configuration c = new Configuration();
      Configuration c2 = c.configure("my/hibernate2.cfg.xml");
      // String[] s=c2.generateSchemaCreationScript(MySQLDialect() );
      // System.out.println(s);
      Iterator i = c.getClassMappings();
      while (i.hasNext()) {
         Object o = i.next();
         System.out.println(o);
         RootClass r = (RootClass) o;
         String cn=r.getClassName();
         System.out.println(cn);
         String en=r.getEntityName();
         System.out.println(en);
         KeyValue kv=r.getKey();
         Table rt=r.getRootTable();
         Table t=r.getTable();
         
         System.out.println("PrimaryKey:");
         PrimaryKey pk= t.getPrimaryKey();
         System.out.println(pk);
         String pkname=pk.getName();
         System.out.println(pkname);
         Table pkt=pk.getTable();
         List pkc=pk.getColumns();
         ListIterator pkcli=pkc.listIterator();
         System.out.println("Columnas de la pk");
         while(pkcli.hasNext()){
            o=pkcli.next();
            Column col=(Column) o;
            System.out.println(col);
            System.out.println(col.getCanonicalName()+" "+col.getName()+" "+col.getSqlType());
         }
         Iterator fkit=t.getForeignKeyIterator();
         while(fkit.hasNext()){
            o=fkit.next();
            System.out.println();
         }
      }
      SessionFactory sf = c2.buildSessionFactory();
      Session s=sf.openSession();
      
      Map m=s.getSessionFactory().getAllClassMetadata();
      for(Object k:m.keySet()){
         Class cl= k.getClass();
         System.out.println(cl);
         String key=(String) k;
         Object val=m.get(key);
         SingleTableEntityPersister step=(SingleTableEntityPersister) val;
         System.out.println();
      }

   }


Top
 Profile  
 
 Post subject: Re: Get FK y PK from hibernate API
PostPosted: Wed Mar 10, 2010 1:08 pm 
Newbie

Joined: Wed Mar 10, 2010 7:16 am
Posts: 7
Hi, I have just realised that relational information obtained this way is DB relational information, and what I really need is relations among entity hibernate java objects.

Can anybody help?

thanks


Top
 Profile  
 
 Post subject: Re: Get FK y PK from hibernate API
PostPosted: Wed Mar 10, 2010 4:35 pm 
Regular
Regular

Joined: Thu May 07, 2009 5:56 am
Posts: 94
Location: Toulouse, France
hi, I'm not sure I understand what do you do. Can you please explain with more details? when you say "I have to display data entities and its relations with other entities" you not really need explore Hibernate metadata to get relations among entities. e.g. you can introspect your entity and extract fields and annotations (if you're using it). no?

_________________
everything should be made as simple as possible, but not simpler (AE)


Top
 Profile  
 
 Post subject: Re: Get FK y PK from hibernate API
PostPosted: Wed Mar 10, 2010 5:22 pm 
Newbie

Joined: Wed Mar 10, 2010 7:16 am
Posts: 7
I explain my problem:
- I use a grid to show some entity (=hibernate class=db table). Let's say that we have a grid of "projects", each row is a project.
- Some of the fields of this grid are some Foreing Key to other entity. Let's say, "Project leader" field that points to another entity/table of "people".
- User can edit "Project grid", most fields are easy to handle, but when he wants to change the "project leader" the "people grid" opens to let choose one of them.
- This grid component is not bound to "projects" or "people", so there is no way to hardcode this. It dinamically shows attributes/fields of the entity being shown. So there is no way to hardcode dependencies.

Above is the reason why I need to discover keys and relations between entities at runtime.

Above you said that this could be done with <introspect>. Could you explain how could it be done? You are referring to reflection? I do not know how to do it, I would thank some help or some tip of where to look for...

Thanks a lot,
txemi.


Top
 Profile  
 
 Post subject: Re: Get FK y PK from hibernate API
PostPosted: Wed Mar 10, 2010 5:41 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
SessionFactory.getClassMetadata() gives you a ClassMetadata object for a given entity class. It has a lot of information about all defined properties. For example, ClassMetadata.getPropertyTypes() give you an array containing the Type definition of all properties.


Top
 Profile  
 
 Post subject: Re: Get FK y PK from hibernate API
PostPosted: Wed Mar 10, 2010 6:11 pm 
Regular
Regular

Joined: Thu May 07, 2009 5:56 am
Posts: 94
Location: Toulouse, France
ok, I think I begin to understand you requirement..

Quote:
- I use a grid to show some entity (=hibernate class=db table)

how do you populate your grid? JDBC (rows from database) or object approach (Hibernate query: "select p from projects as p")?

Quote:
- This grid component is not bound to "projects" or "people", so there is no way to hardcode this. It dinamically shows attributes/fields of the entity being shown. So there is no way to hardcode dependencies.

I understand that, you want a general viewer/editor of entities. that's why I think you try to use Introspection approach (this can make your job easier). take a look at http://java.sun.com/docs/books/tutorial ... index.html

on the other hand, if you use JDBC approach (ResultSet) you should explore Hibernate mapping to locate the column name that correspond to the cell you're editing then to find referenced entity (table?)...I think you're right to use c.getClassMappings() as posted above.

Quote:
Above you said that this could be done with <introspect>

sorry for 'introspect' (so french). It's Introspection. but see also http://java.sun.com/developer/technical ... eflection/

_________________
everything should be made as simple as possible, but not simpler (AE)


Top
 Profile  
 
 Post subject: Re: Get FK y PK from hibernate API
PostPosted: Thu Mar 11, 2010 8:41 pm 
Newbie

Joined: Mon Nov 19, 2007 9:01 am
Posts: 8
I don't think you can get a Configuration object from the SessionFactory. If you're using Spring you can get it from the Spring factory bean like this:

Code:
   protected @Resource(name="&sessionFactory") org.springframework.orm.hibernate3.LocalSessionFactoryBean lsfb;
   Configuration config = lsfb.getConfiguration();

where "sessionFactory" is whatever your SessionFactory bean name is. The "&" tells Spring to give you the Spring factory bean itself, not the Hibernate SessionFactory bean that it would normally create.

Otherwise you need to create the Configuration object yourself from the XML files. Ideally you'd do that at app startup time and use that same object to feed into the instantiation of the SessionFactory.


Top
 Profile  
 
 Post subject: Re: Get FK y PK from hibernate API
PostPosted: Fri Mar 12, 2010 4:31 am 
Newbie

Joined: Wed Mar 10, 2010 7:16 am
Posts: 7
Thanks very much, object ClassMetadata and looks nice. I get relations between entities, type of relation (oneToOne...), and type returned. I will have a look for PK too. I am playing with it at the moment, I will try to make the grids I need and I will report later here. I suppose I will need reflection later to manage objects with this information.
Thanks again.

Code:
      ClassMetadata m = s.getSessionFactory().getClassMetadata(className);
      for (String pn : m.getPropertyNames()) {
         System.out.println("==========" + pn);
         Type t = m.getPropertyType(pn);
         System.out.println(t.getClass());
         System.out.println(t.getReturnedClass());
         System.out.println(t.isAssociationType());
         System.out.println(t.isEntityType());
}


Top
 Profile  
 
 Post subject: Re: Get FK y PK from hibernate API
PostPosted: Mon Mar 15, 2010 6:59 am 
Newbie

Joined: Wed Mar 10, 2010 7:16 am
Posts: 7
Hi, I am using ClassMetadata for obtaining these information.

With getPropertyType and getIdentifierType I get Type objects with lot of information, but not succeed int gaining access to FK information. I mean, In hibernate xml file:

Code:
      <id name="FKfield" type="string">
         <column name="FKtableField" />
         <generator class="foreign">
            <param name="property">param1</param>
         </generator>
      </id>
      <one-to-one name="param1" class="com.bla.bla.myclass"
         constrained="true" cascade="all">
      </one-to-one>   


I can get a Type object for FKfield with getIdentifierType, but after having a look to Type methods I do not know how to access to foreing generator in XML file to know that this field is an FK. Any hint?


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