-->
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.  [ 7 posts ] 
Author Message
 Post subject: Adding virtual ID / Dummy fields as primary key.
PostPosted: Mon Feb 05, 2007 3:50 am 
Beginner
Beginner

Joined: Tue Jan 30, 2007 1:35 pm
Posts: 27
Hi All,

I'm new in Hibernate, may I know is that possible to add a virtual ID as primary during reverse engineering ?

I go thru the reverse engineering on overiding the primary key and tried out the examples. When I try to put a primary key that is not exist in the column..it will prompt error. Please give me some advice. Thanks in advacnce.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 05, 2007 12:45 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
you can add all the columns/keys you want to hibernate mappings (reverse engineering or not) but of course the data for column needs to come somewhere.....namely the table and/or view you are mapping.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 05, 2007 1:30 pm 
Beginner
Beginner

Joined: Tue Jan 30, 2007 1:35 pm
Posts: 27
I try to overide the getPrimaryKeyColumnNames to return 1 column name. May I know what API I can use the return that ? Thanks in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 06, 2007 10:45 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
i do not understand your question.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 06, 2007 3:05 pm 
Beginner
Beginner

Joined: Tue Jan 30, 2007 1:35 pm
Posts: 27
Sorry for confuse you. My question is how can I get the name of first column in the table I specify in hibernate.reveng.xml ? for e.g :

in hibernate.reveng.xml, I put :

<table-filter match-schema="SONICA" match-name="SYS_ADJ"/>

When I run reverse engineering tools, I will get all the fields in my table (ie: SYS_ADJ). How can the name of first column ? The purpose for this is I want to set the first column as my primary key.

Thanks for your advice.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 07, 2007 2:15 pm 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
Here is a hack to get access to the currently analyzed Table object in a ReverseEngineeringStrategy class:
Code:
public class Strategy extends DelegatingReverseEngineeringStrategy {

   // Instance to hold the Table object describing the table on which work is
   // currently being performed. This is necessary because we need to suppress
   // the primary keys from being mapped to the pojos, but we have no access to
   // the information in the meta attribute routines. Valid as long as
   // isManyToMany is called first in the binder's createPersistentClasses.
   private org.hibernate.mapping.Table currentTable;

   public Strategy(ReverseEngineeringStrategy delegate) {
      super(delegate);
   }

   @Override
   public boolean isManyToManyTable(Table table) {
      // Store the Table object for further analysis down the road.
      currentTable = table;
      return super.isManyToManyTable(table);
   }

   @Override
   public Map columnToMetaAttributes(TableIdentifier identifier, String column) {
      Map<String, MetaAttribute> metaAttributes = new HashMap<String, MetaAttribute>();
      String identifierName = "";

      // Find the primary key column name from the stored Table object
      if (currentTable != null && currentTable.hasPrimaryKey()) {
         List columns = currentTable.getPrimaryKey().getColumns();
         if (columns.size() == 1) {
            Column keyColumn = (Column) columns.get(0);
            identifierName = keyColumn.getName();
         }
      }

      // Suppress pojo mapping of primary keys and specified column names
      if ((!inForeignKeyRoutine && identifierName.equalsIgnoreCase(column))
            || (Arrays.binarySearch(excludePojoColumnNames, column) >= 0)) {
         MetaAttribute genProperty = new MetaAttribute("gen-property");
         genProperty.addValue("false");
         metaAttributes.put("gen-property", genProperty);
      }

      return metaAttributes;
   }
}

Please note that this is a hack to expose the Table object to the rest of the ReverseEngineeringStrategy methods (columnToMetaAttributes in this case) and is only valid so long as Max keeps the isManyToMany call the first Strategy method employed in the binder's createPersistentClasses method. This works on on beta8, but is not guaranteed to work on any subsequent releases. There is also no guarantee that the Table object will be fully hydrated with complete information at this stage in the reverse engineering process, so use with caution.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 08, 2007 5:24 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
hmm....maybe we should let a revengstrategy access the MetaDataDialect (or even some kind of CachedMetaDataDialect for performance sake) ..... if you feel that will help submit a jira for it.

_________________
Max
Don't forget to rate


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