-->
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.  [ 27 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Control autogenerating of mapping files
PostPosted: Wed Apr 25, 2007 9:47 am 
Regular
Regular

Joined: Thu Apr 05, 2007 7:05 am
Posts: 53
Location: Troisdorf, Germany
Hello,

I get from Hibernate Tools this mapping file:

Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Apr 23, 2007 4:53:05 PM by Hibernate Tools 3.2.0.beta8 -->
<hibernate-mapping>
    <class name="de.fhg.scai.bio.csr.io.database.hibernate.Item" table="ITEM">
        <id name="id" type="int">
            <column name="ID" precision="22" scale="0" />
            <generator class="assigned" />
        </id>
        <property name="type" type="string">
            <column name="TYPE" length="45" />
        </property>
        <set name="moleculeses" inverse="true">
            <key>
                <column name="ITEM_ID" precision="22" scale="0" not-null="true" unique="true" />
            </key>
            <one-to-many class="de.fhg.scai.bio.csr.io.database.hibernate.Molecules" />
        </set>
    </class>
</hibernate-mapping>


Bit instead of
Code:
<generator class="assigned" />

I want
Code:
<generator class="sequence">
            <param name="sequence">item_seq</param>
         </generator>


The problem is that I have a lot of tables where I always have the same problem. Every time the primary key name is "id" and type is "INTEGER". The name of the sequence is always "<tablename>_seq". I know I could write a skript that changes this for me, but it would be the best if hibernate could do this directly for me.

Greetz Carina


Hibernate version: 3.2 beta 8

Name and version of the database you are using: Oracle 10g

Create Statement of the tables:
Code:
CREATE TABLE item (
  id INTEGER  NOT NULL ,
  type  VARCHAR(45) NULL,
  PRIMARY KEY(id)
);

CREATE TABLE molecules (
  item_id INTEGER  NOT NULL,
  name VARCHAR(100) NULL,
  smiles VARCHAR(100) NULL,
  iupac VARCHAR(100) NULL,
  inchi VARCHAR(100) NULL,
  formula VARCHAR(100) NULL,
  createBy VARCHAR(100) NULL,
  nofAtoms INT NULL,
  nofBonds INT NULL,
  nofHeavyAtoms INT NULL,
  nofHeavyBonds INT NULL,
  nofFragments INT NULL,
  nofSuperAtoms INT NULL,
  nofRings INT NULL,
  nofRGroups INT NULL,
  medianBondLength DOUBLE PRECISION NULL,
  mass DOUBLE PRECISION NULL,
  directory VARCHAR(100) NULL,
  workflowID INTEGER  NULL,
  PRIMARY KEY(item_id),
  FOREIGN KEY(item_id)
    REFERENCES item(id)
);

CREATE SEQUENCE item_seq
start with 1
increment by 1
nomaxvalue;


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 25, 2007 9:52 am 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
Use a ReverseEngineeringStrategy class and override getTableIdentifierStrategyName to return "sequence" and override getTableIdentifierProperties to return tableIdentifier.getName() + "_seq"


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 25, 2007 10:13 am 
Regular
Regular

Joined: Thu Apr 05, 2007 7:05 am
Posts: 53
Location: Troisdorf, Germany
I thought that I could solve it this way, but the documentation of the ReverseEngineeringStrategy doesn't really help me. For first try I copied just the example Example but directly eclipse says to me that DelegatingReverseEngineeringStrategy, ReverseEngineeringStrategy and TableIdentifier could not resolce to a type. So, do I have to make some changes in the classpath? Hibernate.jar is in there, so I can't imagine why eclipse give this error message.

Code:
package de.fhg.scai.bio.csr.io.database.communication;

public class REStrategy extends DelegatingReverseEngineeringStrategy {

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

    public String columnToPropertyName(TableIdentifier table, String column) {
       if(column.endsWith("PK")) {
          return "id";
       }
       else {
          return super.columnToPropertyName(table, column);
       }
    }
}
[/url]


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 25, 2007 10:22 am 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
hibernate-tools.jar needs to be in your classpath.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 25, 2007 10:31 am 
Regular
Regular

Joined: Thu Apr 05, 2007 7:05 am
Posts: 53
Location: Troisdorf, Germany
Yes, that looks much better. ^^


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 25, 2007 10:46 am 
Regular
Regular

Joined: Thu Apr 05, 2007 7:05 am
Posts: 53
Location: Troisdorf, Germany
Okay, I'm sure that you think I'm terrible annoying but I have the next problem.

When I want to override the getTableIdentifierProperties function, the normal return type is Properties, but I just have a String. So I wanted to make a Properties-object and put the String in it, but I need a key, value pair to insert. So, what is the key I have to use? Or do I have to make something else?

Code:
    public Properties getTableIdentifierProperties(TableIdentifier identifier) {
       Properties p = new Properties();
       p.put(key, identifier.getName() + "_seq");
       return p;
    }
}


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 25, 2007 11:18 am 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
Oops, I forgot that returned a properties. The key is the param name, in this case, "sequence". The generator than should have <param name="sequence">MYTABLE_SEQ</param>


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 25, 2007 11:36 am 
Regular
Regular

Joined: Thu Apr 05, 2007 7:05 am
Posts: 53
Location: Troisdorf, Germany
It'll be a long day...
When I want to use it, I get this message:

Image

Code:
package de.fhg.scai.bio.csr.io.database.communication;

import java.util.Properties;

import org.hibernate.cfg.reveng.DelegatingReverseEngineeringStrategy;
import org.hibernate.cfg.reveng.ReverseEngineeringStrategy;
import org.hibernate.cfg.reveng.TableIdentifier;


public class REStrategy extends DelegatingReverseEngineeringStrategy {

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

    public String columnToPropertyName(TableIdentifier table, String column) {
       if(column.endsWith("PK")) {
          return "id";
       }
       else {
          return super.columnToPropertyName(table, column);
       }
    }
   
    public String getTableIdentifierStrategyName(TableIdentifier tableIdentifier) {
       return "sequence";
    }
   
    public Properties getTableIdentifierProperties(TableIdentifier identifier) {
       Properties p = new Properties();
       p.put("sequence", identifier.getName() + "_seq");
       return p;
    }
   
}


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 25, 2007 11:44 am 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
Hehe...sometimes things just happen in waves like that. Make sure the compiled REStrategy class is in the console configuration classpath.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 25, 2007 11:56 am 
Regular
Regular

Joined: Thu Apr 05, 2007 7:05 am
Posts: 53
Location: Troisdorf, Germany
I inserted it in the configuration wizard as reverse strategy. When I click on the browse button, there is directly and only this class to choice: org.hibernate.cfg.reveng.ReverseEngineeringStrategyUtil

When I insert there instead the package and the file name of my strategy I get also a error:
Could not create or find <pathToMyStrategy> with one argument delegate constructor.

When I use the ant-build functionality of the project I'm working on I get this message: package org.hibernate.cfg.reveng does not exist. But I added it to the class path of my project. Do I have to add it somewhere else?

---

SOLVED this problem on my own. I had to copy the hibernate-tool.jar into a directory in my project and not reference on the one in the plugin directory of eclipse.
Thank you a lot for all your help! That was really great. I think I have to mention you in my bachelor thesis. ^^


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 26, 2007 5:10 am 
Regular
Regular

Joined: Thu Apr 05, 2007 7:05 am
Posts: 53
Location: Troisdorf, Germany
--- deleted ---

My boss just told me that I there will be a lot of people inserting data into the database at once. So I can't use the possibility to generate the primary keys on the program side.


Last edited by N-te Jr. on Thu Apr 26, 2007 6:33 am, edited 2 times in total.

Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 26, 2007 5:51 am 
Regular
Regular

Joined: Thu Apr 05, 2007 7:05 am
Posts: 53
Location: Troisdorf, Germany
--- deleted ---


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 26, 2007 10:15 am 
Regular
Regular

Joined: Thu Apr 05, 2007 7:05 am
Posts: 53
Location: Troisdorf, Germany
After a lot of work I found out that when I say session.save() in this moment the sequence is asked for the next key and the good thing is that the next one asking gets the next value. Its uninteresting at which moment the data is written.
I don"t know if you can follow my argumentation but I think it's transaction safe. *g*

But one problem is left, which I deleted before because I thought I couldn't use this stuff anymore. At the moment my Strategy generates the mapping-code for using a sequence for every table. But thats not right. It should only be produced when the name of the key is id. I tried it but always got a NullPointerException because of the line super.getPrimaryKeyColumnNames(tableIdentifier).contains("ID").

Code:
   public Properties getTableIdentifierProperties(TableIdentifier tableIdentifier) {
      if (super.getPrimaryKeyColumnNames(tableIdentifier).contains("ID")) {
         Properties p = new Properties();
         p.put("sequence", tableIdentifier.getName().toLowerCase() + "_seq");
         return p;
      } else {
         return super.getTableIdentifierProperties(tableIdentifier);
      }
   }


A second problem I have is that I also have mapping files which look like that:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Apr 26, 2007 4:05:11 PM by Hibernate Tools 3.2.0.beta8 -->
<hibernate-mapping>
    <class name="de.fhg.scai.bio.csr.io.database.hibernate.Atoms" table="ATOMS">
        <composite-id name="id" class="de.fhg.scai.bio.csr.io.database.hibernate.AtomsId">
            <key-property name="id" type="int">
                <column name="ID" precision="22" scale="0" />
            </key-property>
            <key-many-to-one name="molecules" class="de.fhg.scai.bio.csr.io.database.hibernate.Molecules">
                <column name="MOLECULES_ITEM_ID" precision="22" scale="0" />
            </key-many-to-one>
        </composite-id>
        <property name="atomlabel" type="string">
            <column name="ATOMLABEL" length="6" />
        </property>
    </class>
</hibernate-mapping>


How can I use my sequences there?

Carina


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 26, 2007 10:42 am 
Senior
Senior

Joined: Sat Apr 21, 2007 11:01 pm
Posts: 144
I was also having simalr porblems to you, I think the best way to go is to use a native primary key, which is something I'm trying to do for all tables in this post: http://forum.hibernate.org/viewtopic.php?t=973806
You can see the helpfull advice I got from Max there. ;-)
I'd look at the source code but I can't get access to it from work... lol


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 26, 2007 11:14 am 
Regular
Regular

Joined: Thu Apr 05, 2007 7:05 am
Posts: 53
Location: Troisdorf, Germany
I think I can't use that because I need a transaction safe program because a lot of different threads are writing into my database at the same time. Thats why I need the sequences.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 27 posts ]  Go to page 1, 2  Next

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.