aspose file tools*
The moose likes Object Relational Mapping and the fly likes create - to generate DB Tables" title="" /> Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Reply locked New topic
Author

"hibernate.hbm2ddl.auto">create - to generate DB Tables

M Marin
Ranch Hand

Joined: Feb 20, 2010
Posts: 32
Hello,

I am developing a program with Hibernate and Oracle and I want to reproduce the same application using MySQL. I want to generate de database tables using:

<property name="hibernate.hbm2ddl.auto">create</property>

from the ("Oracle") domain code, but I can only create one table out of four. Does anyone have an idea what I am doing wrong?

Regards.

M. Marin.
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

We'll probably need a bit more information. Are you getting any errors?

If you can do one, then try to do just two. See if you can get two to work. Maybe even scale it back to table name and primary key for two tables. Then incrementally do more until you find the setting that might be causing the problem.

Just an approach. Any errors? Bizarre table mappings? I'm betting the three tables are dependent on each other?
M Marin
Ranch Hand

Joined: Feb 20, 2010
Posts: 32
Thank you for the answer. I followed the instructions and It helped to locate the problem. Oracle allowed me to use the word "KEY" as a field name and MySQL did not like it, so I guess "KEY" is a reserved keyword in MySQL. So I changed the field name and now it works.

from:

@Length(max=80)
@Column(name="KEY", length=80)
public String getKey() {
return key;
}


public void setKey(String key) {
this.key = key;
}



to:

@Length(max=80)
@Column(name="PKEY", length=80)
public String getPkey() {
return pkey;
}


public void setPkey(String pkey) {
this.pkey = pkey;
}

now it works, so great. Thank you for the quick help.

M. Marin.
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

Good sleuthing, Greenhorn! And thanks for the post-back. You'll be surprised by how many people will have that exact problem and have it subsequently solved by your hard work.

I know a guy that wrote an entire book using a table named "User" and a MySQL database, only to discover none of the examples worked as promised in certain databases because User is a reserved word. What a drag!

-Cameron McKenzie

rajasekhar kannamaneni
Greenhorn

Joined: Nov 05, 2010
Posts: 27
M Marin wrote:Hello,

I am developing a program with Hibernate and Oracle and I want to reproduce the same application using MySQL. I want to generate the database tables using:

<property name="hibernate.hbm2ddl.auto">create</property>

from the ("Oracle") domain code, but I can only create one table out of four. Does anyone have an idea what I am doing wrong?

Regards.

M. Marin.


Hi,

can you please post your bean class and persistence.xml and the settings what you are using to execute this program..because I am facing the same problem and struggling for one week to create tables in database automatically.

Thanks in Advance,


Best Regards,
Raja.
M Marin
Ranch Hand

Joined: Feb 20, 2010
Posts: 32
I am not using persistance.xml for the project. I use hibernate.cfg.xml:

(This is a simplified version)

<?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="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">mypassword</property>
<property me="hibernate.connection.url">jdbc:mysql://localhost:3306/mydb</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.search.autoregister_listeners">false</property>
<property name="hibernate.hbm2ddl.auto">create</property>
<property name="hibernate.show_sql">true</property>
<mapping class="data.Customer" />
</session-factory>
</hibernate-configuration>

Try whether that works

Marin
M Marin
Ranch Hand

Joined: Feb 20, 2010
Posts: 32
Add also the schema name manually to de database, just leave the tables to be automatically generated.
rajasekhar kannamaneni
Greenhorn

Joined: Nov 05, 2010
Posts: 27
M Marin wrote:Add also the schema name manually to de database, just leave the tables to be automatically generated.


I did what you said..but still its not creating any tables in database and its printing this on console:

10:24:07,513 INFO [SessionFactoryObjectFactory] Bound factory to JNDI name: persistence.unit:unitName=ernst-app-ear-1.0.ear/persistenceImpl-app-1.0.war#DefaultDS
10:24:07,513 WARN [SessionFactoryObjectFactory] InitialContext did not implement EventContext
10:24:07,528 INFO [SchemaUpdate] Running hbm2ddl schema update
10:24:07,528 INFO [SchemaUpdate] fetching database metadata
10:24:07,528 INFO [SchemaUpdate] updating schema
10:24:07,528 INFO [SchemaUpdate] schema update complete
10:24:07,544 INFO [NamingHelper] JNDI InitialContext properties:{java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory, java.naming.factory.url.pkgs=org.jboss.namingrg.jnp.interfaces}

can you please tell me what you have done other than creating hbm.xml, persistence.xml and entitiy beans..can you please check it.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336


10:24:07,528 INFO [SchemaUpdate] updating schema
10:24:07,528 INFO [SchemaUpdate] schema update complete

Looks like its updating the schema.


(NB: "entity beans" are a EJB2 technology. When you say you created entity beans did you really mean entity beans? )


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
rajasekhar kannamaneni
Greenhorn

Joined: Nov 05, 2010
Posts: 27
Paul Sturrock wrote:

10:24:07,528 INFO [SchemaUpdate] updating schema
10:24:07,528 INFO [SchemaUpdate] schema update complete

Looks like its updating the schema.


(NB: "entity beans" are a EJB2 technology. When you say you created entity beans did you really mean entity beans? )


hi, this is my class..can you please check it..

rajasekhar kannamaneni
Greenhorn

Joined: Nov 05, 2010
Posts: 27
M Marin wrote:Add also the schema name manually to de database, just leave the tables to be automatically generated.



what I found one interesting fact is..when jboss runs persistence.xml its not generating any create table queries..thats why its not creating any tables..how to generate the create table queries automatically.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

This appears to be a duplicate. Please continue the discussion in your other topic.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: "hibernate.hbm2ddl.auto">create - to generate DB Tables