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 ?