-->
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.  [ 5 posts ] 
Author Message
 Post subject: Help creating PK: dialect.getAddPrimaryKeyConstraintString
PostPosted: Tue Sep 28, 2010 11:57 am 
Newbie

Joined: Fri Feb 27, 2009 8:02 am
Posts: 10
Location: Woking, England
Hi,
I am using the <hbm2ddl> exporter and the InformixDialect (Hibernate321)
I have an annotated class with the following primary key defined:-

Code:
@Table(name = "MYTABLE")
public class MyTable {
  private Integer id;

  @Id
  @Column(name = "ID")
  public Integer getId() {
    return id;
  }
}


I'm launching the ANT task as follows:-

Code:
<hbm2ddl delimiter=";" format="true" create="true" />


The generated DDL is as follows:

Code:
create table MYTABLE (
  ID  serial not null,
  primary key (ID)
);


But, what I want is (primary key added after the create table - supported in the Dialect):
Code:
create table MYTABLE (
  ID  serial not null
);

alter table MYTABLE add constraint primary key (ID) constraint MYTABLE_PK;


Hibernate's PrimaryKey class defines two sqlConstraintString methods:
- one returns the string "primary key (columnname)" as used in the generated DDL above.
- the other returns the string from dialect.getAddPrimaryKeyConstraintString(constraintName).
Which in InformixDialect.java is: " add constraint primary key constraint " + constraintName + " ";

It's the latter method that I want to use - how do I annotate my class to acheive this?

Many thanks, Martin.

_________________
Thanks in advance for any helpful replies :O)


Top
 Profile  
 
 Post subject: Re: Help creating PK: dialect.getAddPrimaryKeyConstraintString
PostPosted: Wed Sep 29, 2010 11:04 am 
Newbie

Joined: Fri Feb 27, 2009 8:02 am
Posts: 10
Location: Woking, England
Hi,

Thanks for replying, however I have specified the Dialect; in fact we have overridden the NUMERIC type from decimal to decimal(13,3) and the generated DDL reflects this change proving that the dialect is being used.

Any other ideas?

Martin.

_________________
Thanks in advance for any helpful replies :O)


Top
 Profile  
 
 Post subject: Re: Help creating PK: dialect.getAddPrimaryKeyConstraintString
PostPosted: Wed Sep 29, 2010 11:32 am 
Newbie

Joined: Fri Feb 27, 2009 8:02 am
Posts: 10
Location: Woking, England
Hi,

Thanks for replying, however I have specified the Dialect; in fact we have overridden the NUMERIC type from decimal to decimal(13,3) and the generated DDL reflects this change proving that the dialect is being used.

Any other ideas?

Martin.

_________________
Thanks in advance for any helpful replies :O)


Top
 Profile  
 
 Post subject: Re: Help creating PK: dialect.getAddPrimaryKeyConstraintString
PostPosted: Thu Sep 30, 2010 5:38 am 
Newbie

Joined: Fri Feb 27, 2009 8:02 am
Posts: 10
Location: Woking, England
Hi,

So you are able to generate a create table with the primary key outside of the create table, as an "alter table add constraint" as per the dialect?

Here is my relevant code:

My hibernate.cfg.xml
Code:
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>
        <property name="dialect">org.hibernate.dialect.InformixDialect</property>
        <mapping class="com.example.AccountType" />
    </session-factory>
</hibernate-configuration>


com.example.AccountType.java
Code:
package com.example;
import javax.persistence.*;

@Entity
@Table(name="Account_Type")
public class AccountType {
   public Integer id;

   @Id
   @Column(name = "Id")
   public Integer getId() {
      return id;
   }

   public void setId(Integer id) {
      this.id = id;
   }
}


My ANT build.xml
Code:

     <target name="CreateHibernateTables" description="Generate Create Tables" depends="BuildProject">
        <taskdef name="hibernatetool"
           classname="org.hibernate.tool.ant.HibernateToolTask">   
         <classpath refid="project.class.path" />
         <classpath refid="hibernate352.tools.class.path" />
      </taskdef>
        <hibernatetool destdir="${src}">
          <classpath path="${ant.class.path}" />
         <annotationconfiguration configurationfile="${src}/hibernate.cfg.xml" />
            <hbm2ddl delimiter=";" format="true" export="false" drop="false" create="true" outputfilename="../dump.sql" />       
      </hibernatetool>
     </target>



My generated dump.sql
Code:
    create table Account_Type (
        Id integer not null,
        Description char(15) not null,
        primary key (Id)
    );


Thanks for your help, Martin.

_________________
Thanks in advance for any helpful replies :O)


Top
 Profile  
 
 Post subject: Re: Help creating PK: dialect.getAddPrimaryKeyConstraintString
PostPosted: Fri Oct 01, 2010 7:19 am 
Newbie

Joined: Fri Feb 27, 2009 8:02 am
Posts: 10
Location: Woking, England
InformixDialect.java has a method that returns this syntax:

Code:
   /**
    * The syntax used to add a primary key constraint to a table.
    * Informix constraint name must be at the end.
    * @return String
    */
   public String getAddPrimaryKeyConstraintString(String constraintName) {
      return " add constraint primary key constraint " + constraintName + " ";
   }


and PrimaryKey.java references it:
Code:
public class PrimaryKey extends Constraint {

   public String sqlConstraintString(Dialect dialect) {
      StringBuffer buf = new StringBuffer("primary key (");
      Iterator iter = getColumnIterator();
      while ( iter.hasNext() ) {
         buf.append( ( (Column) iter.next() ).getQuotedName(dialect) );
         if ( iter.hasNext() ) buf.append(", ");
      }
      return buf.append(')').toString();
   }

   public String sqlConstraintString(Dialect dialect, String constraintName, String defaultCatalog, String defaultSchema) {
      StringBuffer buf = new StringBuffer(
         dialect.getAddPrimaryKeyConstraintString(constraintName)
      ).append('(');
      Iterator iter = getColumnIterator();
      while ( iter.hasNext() ) {
         buf.append( ( (Column) iter.next() ).getQuotedName(dialect) );
         if ( iter.hasNext() ) buf.append(", ");
      }
      return buf.append(')').toString();
   }
}

_________________
Thanks in advance for any helpful replies :O)


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