-->
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.  [ 3 posts ] 
Author Message
 Post subject: How to Add unique key in Create Command
PostPosted: Sun Aug 18, 2013 2:51 pm 
Newbie

Joined: Sun Aug 18, 2013 2:39 pm
Posts: 2
Hi All,

I am new to Hibernate.
My database is SQLLite and it has few constraint like
it does not allow adding constraints using alter table command.

As Hibernate by default creates a table and then alters it i am unable to add unique key constraint to the table
for ref my config files are as below :
Code:
Config File ----
<?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>

    <!-- hibernate dialect -->
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>

   
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost/test</property>
    <property name="hibernate.connection.username">sonu</property>
    <property name="hibernate.connection.password">sonu123</property>
  <!--   <property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property> -->
   
    <!-- Automatic schema creation (begin) === -->     
    <property name="hibernate.hbm2ddl.auto">create</property>

    <!--hibernate.cfg.xml -->
   <property name="show_sql">true</property>
   
    <!-- Simple memory-only cache -->
   <!--  <property name="hibernate.cache.provider_class">org.hibernate.cache.HashtableCacheProvider</property> -->

     <!-- Enable Hibernate's automatic session context management -->
<!--     <property name="current_session_context_class">thread</property> -->

    <!-- ############################################ -->
    <!-- # mapping files with external dependencies # -->
    <!-- ############################################ -->


    <mapping resource="com/sample/Person.hbm.xml"/>

</session-factory>
</hibernate-configuration>


hbm file :----
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">


<hibernate-mapping>

   <class name="com.sample.Person" table="Person">

      <id name="id" column="ID">
         <generator class="native" />
      </id>

        <property name="name">
         <column name="NAME" length="16" not-null="true" unique="true"/>
       </property>
      
             <property name="surname">
         <column name="SURNAME" length="16" not-null="true" />
      </property>

      <property name="address">
         <column name="ADDRESS" length="16" not-null="true" />
      </property>

   </class>
</hibernate-mapping>


and the generated SQL query is as below :

Hibernate: drop table if exists Person
Hibernate: create table Person (ID bigint not null auto_increment, NAME varchar(16) not null, SURNAME varchar(16) not null, ADDRESS varchar(16) not null, primary key (ID))
Hibernate: alter table Person add constraint UK_b1dd3vidhowfkune7svm0uh0b unique (NAME)

Please let me know if it is possible to define unique key in the create table query it self ?


Top
 Profile  
 
 Post subject: Re: How to Add unique key in Create Command
PostPosted: Wed Aug 21, 2013 4:06 am 
Beginner
Beginner

Joined: Wed Feb 06, 2013 2:43 am
Posts: 46
Hope this helps :

<property name="name" length="16" not-null="true" unique="true">
<column name="NAME"/>
</property>

_________________
Thanks,
Ajit Singh
ajits@mindfiresolutions.com
www.mindfiresolutions.com


Top
 Profile  
 
 Post subject: Re: How to Add unique key in Create Command
PostPosted: Wed Aug 21, 2013 6:10 am 
Newbie

Joined: Sun Aug 18, 2013 2:39 pm
Posts: 2
Adding like
Code:
<property name="name" length="16" not-null="true" unique="true">
         <column name="NAME" />
       </property>


Just runs the create table without alter
Hibernate: create table Person (ID bigint not null auto_increment, NAME varchar(255), SURNAME varchar(16) not null, ADDRESS varchar(16) not null, primary key (ID))

It does not solve the problem of adding unique key to the table


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