aspose file tools*
The moose likes Object Relational Mapping and the fly likes How to Create Auto Schema Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "How to Create Auto Schema" Watch "How to Create Auto Schema" New topic
Author

How to Create Auto Schema

rajasekhar kannamaneni
Greenhorn

Joined: Nov 05, 2010
Posts: 27
Hi,

I have created a package which contains all entity beans and then corresponding persistence.xml file..when I deploy EAR file in JBOSS..its not creating the schema automatically..is there any way to achieve this.

Thanks in Advance,


Best Regards,
Rajasekhar.
James Sutherland
Ranch Hand

Joined: Oct 01, 2007
Posts: 553
This depends on which JPA provider you are using. The JPA does not define auto schema creation.

For EclipseLink you set the persistence.xml property,

"eclipselink.ddl-generation"="create-tables"


TopLink : EclipseLink : Book:Java Persistence : Blog:Java Persistence Performance
rajasekhar kannamaneni
Greenhorn

Joined: Nov 05, 2010
Posts: 27
James Sutherland wrote:This depends on which JPA provider you are using. The JPA does not define auto schema creation.

For EclipseLink you set the persistence.xml property,

"eclipselink.ddl-generation"="create-tables"


actually I am using Hibernate and JPA

here is my persistence.xml file:


<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
version="1.0">
<persistence-unit name="DefaultDS" transaction-type="JTA">

<jta-data-source>java:/DefaultDS</jta-data-source>

<class>com.ernst.persistenceImpl.beanImpl.ArtikelBean</class>

<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/lagerstandnew"/>

<property name="hibernate.connection.username" value="raja"/>
<property name="hibernate.connection.password" value="sekhar"/>

<!-- Scan for annotated classes and Hibernate mapping XML files -->
<property name="hibernate.archive.autodetection" value="class, hbm"/>
<property name="hibernate.hbm2ddl.auto" value="create"/>
<property name="hibernate.transaction.factory_class"
value="org.hibernate.transaction.JDBCTransactionFactory"/>
<!-- SQL stdout logging -->
<property name="hibernate.format_sql" value="true"/>
<property name="hibernate.show_sql" value="true"/>
<property name="use_sql_comments" value="true"/>
</properties>
</persistence-unit>
</persistence>



but its not generating any autoschema..can you please tell me whats going wrong with it.
Imran Perwez
Greenhorn

Joined: Sep 19, 2010
Posts: 12
Try changing...

To...


See if that helps...
rajasekhar kannamaneni
Greenhorn

Joined: Nov 05, 2010
Posts: 27
Imran Perwez wrote:Try changing...

To...


See if that helps...


If I use above code also its not generating any tables..

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.
Imran Perwez
Greenhorn

Joined: Sep 19, 2010
Posts: 12
Maybe a silly question... but I take it you are using JPA entities that are correctly annotated? And, not the old style EJB2.1 entity bean?

I don't have the issue you have. I have quite a minimalistic persistence.xml file and datasource compared to yours...

peristence.xml...



Datasource in "<jboss>\server\default\deploy"...

rajasekhar kannamaneni
Greenhorn

Joined: Nov 05, 2010
Posts: 27
Imran Perwez wrote:Maybe a silly question... but I take it you are using JPA entities that are correctly annotated? And, not the old style EJB2.1 entity bean?

I don't have the issue you have. I have quite a minimalistic persistence.xml file and datasource compared to yours...

peristence.xml...



Datasource in "<jboss>\server\default\deploy"...



actually I am using almost similar specifications made by you..I will tell one more interesting thing..its creating tables automatically when I have *.hbm.xml files but when I dont have these xml files its not creating any tables directly by using entity beans..I dont want to create hibernate matching files..jboss have to create tables just by using my entity tables only..is it possible to do this?
rajasekhar kannamaneni
Greenhorn

Joined: Nov 05, 2010
Posts: 27
for your quick view this is my entity bean..can you please check it whats going wrong in it.

Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336


for your quick view this is my entity bean

Entity Beans are an EJB 2 technology. It confuses the issue if you use the term to refer to JPA classes.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
rajasekhar kannamaneni
Greenhorn

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

for your quick view this is my entity bean

Entity Beans are an EJB 2 technology. It confuses the issue if you use the term to refer to JPA classes.


can you please tell me how can I create the table using above class.
Imran Perwez
Greenhorn

Joined: Sep 19, 2010
Posts: 12
I think it's your transaction strategy that is causing the problem



Have you tried using a different transaction strategy?

rajasekhar kannamaneni
Greenhorn

Joined: Nov 05, 2010
Posts: 27
Imran Perwez wrote:I think it's your transaction strategy that is causing the problem



Have you tried using a different transaction strategy?



I have tried with other transaction factories also..but it will throw classcast exception for it.
Imran Perwez
Greenhorn

Joined: Sep 19, 2010
Posts: 12
What was the exception?...
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Transactions are unlikely to be the problem I think - DDL is (usually) not transactional.

Your configuration refers to something called ArtikelBean, which is not your annotated class. My guess is ArtikelBean does not contain annotations hbm2ddl can use. Can we see the class ArtikelBean, if that is indeed what you are trying to map? I see you have hibernate.archive.autodetection set too, could it be your mapped class is not deployed where you think it is? For auto detection to work, you need to have things packaged pretty much as the specification defines them. Where do you deploy persistence.xml to?
rajasekhar kannamaneni
Greenhorn

Joined: Nov 05, 2010
Posts: 27
Imran Perwez wrote:What was the exception?...


java.lang.ClassCastException: org.hibernate.transaction.TransactionFactoryFactor
y cannot be cast to org.hibernate.transaction.TransactionFactory
at org.hibernate.transaction.TransactionFactoryFactory.buildTransactionF
actory(TransactionFactoryFactory.java:65)
at org.hibernate.cfg.SettingsFactory.createTransactionFactory(SettingsFa
ctory.java:452)
at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:
165)
at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2101
)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.jav
a:1325)

but it will not throw any error by using JTATransactionFactory, but the result is same.
rajasekhar kannamaneni
Greenhorn

Joined: Nov 05, 2010
Posts: 27
Paul Sturrock wrote:Transactions are unlikely to be the problem I think - DDL is (usually) not transactional.

Your configuration refers to something called ArtikelBean, which is not your annotated class. My guess is ArtikelBean does not contain annotations hbm2ddl can use. Can we see the class ArtikelBean, if that is indeed what you are trying to map? I see you have hibernate.archive.autodetection set too, could it be your mapped class is not deployed where you think it is?



hi..later I have replaced the above bean with NrKreiseBean which was pasted in above comment..may be its also a issue..can you please check the above code annotations one more time..its working with hbm.xml file but not with annotated class..can you please look it.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

My guess is you are either using an old version of persistence.xml. Hibernate seems to be reporting it creates a schema without any exceptions occurring, which makes me think your annotated class is not being found.
rajasekhar kannamaneni
Greenhorn

Joined: Nov 05, 2010
Posts: 27
Paul Sturrock wrote:My guess is you are either using an old version of persistence.xml. Hibernate seems to be reporting it creates a schema without any exceptions occurring, which makes me think your annotated class is not being found.


I am using this version:

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
version="1.0">
<persistence-unit name="LagerstandnewDS" transaction-type="JTA">

<jta-data-source>java:/LagerstandnewDS</jta-data-source>

<class>com.ernst.persistenceImpl.beanImpl.NrKreiseBean</class>

<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/lagerstandnew;create=true"/>
<property name="javax.persistence.jdbc.user" value="raja"/>
<property name="javax.persistence.jdbc.password" value="sekhar"/> -->
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/lagerstandnew"/>
<property name="hibernate.connection.username" value="root"/>
<property name="hibernate.connection.password" value="sekhar"/>
<property name="hibernate.archive.autodetection" value="class, hbm"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>

</properties>

</persistence-unit>
</persistence>

Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Is that valid XML? You have a trailing comment.
rajasekhar kannamaneni
Greenhorn

Joined: Nov 05, 2010
Posts: 27
Paul Sturrock wrote:Is that valid XML? You have a trailing comment.


I hope its a valid xml..because if there is any error in xml normally parser will throw the error...what do you think.
Imran Perwez
Greenhorn

Joined: Sep 19, 2010
Posts: 12
This bit, end of line...

rajasekhar kannamaneni
Greenhorn

Joined: Nov 05, 2010
Posts: 27
Imran Perwez wrote:This bit, end of line...



hi, this is my actual xml code and above is due to my mistake while pasting it here.

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
version="1.0">
<persistence-unit name="LagerstandnewDS" transaction-type="JTA">

<jta-data-source>java:/LagerstandnewDS</jta-data-source>

<class>com.ernst.persistenceImpl.beanImpl.NrKreiseBean</class>

<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/lagerstandnew"/>
<property name="hibernate.connection.username" value="root"/>
<property name="hibernate.connection.password" value="sekhar"/>
<property name="hibernate.archive.autodetection" value="class, hbm"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>

<property name="hibernate.id.new_generator_mappings" value="true"/>
<property name="hibernate.generate_statistics" value="true"/>
<property name="hibernate.cache.use_structured_entries" value="true"/>

<property name="hibernate.format_sql" value="true"/>
<property name="hibernate.show_sql" value="true"/>
<property name="use_sql_comments" value="true"/>
</properties>

</persistence-unit>
</persistence>
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How to Create Auto Schema