-->
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: hbm2ddl.auto doesn´t work
PostPosted: Fri Feb 05, 2010 5:42 am 
Newbie

Joined: Fri Feb 05, 2010 5:22 am
Posts: 3
Hello everybody,

I use the hbm2ddl.auto property in my hibernate configuration in order to create a table automatically but unfortunately it doesn´t work.

here is my hibernate configuration:

<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">org.gjt.mm.mysql.Driver</property>
<property name="hibernate.connection.password">Mira</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/adressdb</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<property name="cache.provider.class">org.hibernate.cache.EhCacheProvider</property>
<property name="current_session_context_class">thread</property>
<property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<property name="hbm2ddl.auto">create</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<mapping class="domain.Contact"/>
</session-factory>
</hibernate-configuration>

and here is my mapping class:

@Entity
public class Contact implements Serializable {

private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private String id;
private String name;
private String address;
private String city;
private String state;
private String zipCode;
private String phoneNumber;
private String relationship;

public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
...

When I try to insert a new Contact into the database I get the following error:

Hibernate:
insert
into
Contact
(address, city, name, phoneNumber, relationship, state, zipCode)
values
(?, ?, ?, ?, ?, ?, ?)
Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not insert: [domain.Contact]
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:90)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:64)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2186)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2666)
at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:71)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:279)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:321)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:204)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:130)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:56)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:50)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)
at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:562)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:550)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:546)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:342)
at $Proxy6.save(Unknown Source)
at dao.ContactDAOImpl.addContact(ContactDAOImpl.java:53)
at dao.ContactDAOImpl.main(ContactDAOImpl.java:98)
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'adressdb.contact' doesn't exist
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
at com.mysql.jdbc.Util.getInstance(Util.java:381)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1030)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3558)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3490)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1959)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2109)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2648)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2077)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2362)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2280)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2265)
at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:94)
at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:57)
... 23 more


This error occurres because the table Contact was not created automatically into my Database. But why? I use
<property name="hbm2ddl.auto">create</property>
in my hibernate config file? Anyone see what I´m doing wrong?


Top
 Profile  
 
 Post subject: Re: hbm2ddl.auto doesn´t work
PostPosted: Fri Feb 05, 2010 5:50 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
You probably have also to define

Code:
   <property name="hibernate.archive.autodetection>class"</property>


in order that hibernate scans for annotated classes.
N.B.: Obviously your annotated classes must also be in your java classpath


Top
 Profile  
 
 Post subject: Re: hbm2ddl.auto doesn´t work
PostPosted: Fri Feb 05, 2010 6:03 am 
Newbie

Joined: Fri Feb 05, 2010 5:22 am
Posts: 3
unfortunately the additional
<property name="hibernate.archive.autodetection">class</property>

don´t solve the problem. The annotated class Contact is in my java classpath. Any other ideas?


Top
 Profile  
 
 Post subject: Re: hbm2ddl.auto doesn´t work
PostPosted: Fri Feb 05, 2010 6:37 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Enable

Code:
log4j.logger.org.hibernate.cfg=INFO


in your log4j.properties configuration file and check if a line like is logged:
Code:
... INFO AnnotationBinder:446 - Binding entity from annotated class: domain.Contact


If not , then definitely AnnotationConfiguration does not recognize domain.Contact as persistent class.


Top
 Profile  
 
 Post subject: Re: hbm2ddl.auto doesn´t work
PostPosted: Fri Feb 05, 2010 6:57 am 
Newbie

Joined: Fri Feb 05, 2010 5:22 am
Posts: 3
o.k. now it works! I found the error with a more detailed log output:

11:54:04,543 INFO SchemaExport:251 - exporting generated schema to database
11:54:04,558 ERROR SchemaExport:348 - Unsuccessful: create table Contact (id varchar(255) not null auto_increment, address varchar(255), city varchar(255), name varchar(255), phoneNumber varchar(255), relationship varchar(255), state varchar(255), zipCode varchar(255), user_id integer, primary key (id))
11:54:04,558 ERROR SchemaExport:349 - Incorrect column specifier for column 'id'

The problem was that I had the wrong type for the Id because I use a Generator. I changed the type of the id attribute from String to int and now it works.

Thank you very much!


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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.