PROBLEM 1
I want to create mysql tables from java class using hibernate. For that I called
the sample method in the class CatTest:
public static void create()
{
String s = "CREATE TABLE ATTACHMENT ATTACHMENT_ID numeric NOT NULL,+" +
" DOCUMENT_ID numeric,"+
" ATTACHMEMT_PATH varchar,"+
" ATTACHMENT_SIZE numeric,"+
" CONSTRAINT 'ATTACHMENT_pkey' PRIMARY KEY (ATTACHMENT_ID));";
Session session;
try
{
session = HibernateUtil.getSession();
HibernateUtil.beginTransaction();
session.createSQLQuery(s);
HibernateUtil.commitTransaction();
HibernateUtil.closeSession();
}catch (HibernateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
But it is not working. No exception is thrown. All statements are executed properly.
But the table is not getting created in my database.
In hibernate.properties I have enabled the following properties:
hibernate.dialect org.hibernate.dialect.MySQLDialect
hibernate.connection.driver_class com.mysql.jdbc.Driver
hibernate.connection.url jdbc:mysql:///hib
hibernate.connection.username root
hibernate.connection.password
Tomcat window shows the following output:
11:58:27,265 INFO Environment:464 - Hibernate 3.0.4
11:58:27,265 INFO Environment:482 - loaded properties from resource hibernate.p
roperties: {hibernate.connection.driver_class=com.mysql.jdbc.Driver, hibernate.c
glib.use_reflection_optimizer=true, hibernate.cache.provider_class=org.hibernate
.cache.HashtableCacheProvider, hibernate.max_fetch_depth=1, hibernate.dialect=or
g.hibernate.dialect.MySQLDialect, hibernate.jdbc.use_streams_for_binary=true, hi
bernate.query.substitutions=true 1, false 0, yes 'Y', no 'N', hibernate.connecti
on.username=root, hibernate.cache.region_prefix=hibernate.test, hibernate.connec
tion.url=jdbc:mysql:///hib, hibernate.show_sql=true, hibernate.connection.passwo
rd=****, hibernate.jdbc.batch_versioned_data=true, hibernate.connection.pool_siz
e=1}
11:58:27,265 INFO Environment:509 - using java.io streams to persist binary typ
es
11:58:27,265 INFO Environment:510 - using CGLIB reflection optimizer
11:58:27,265 INFO Environment:540 - using JDK 1.4 java.sql.Timestamp handling
11:58:27,578 INFO Configuration:1110 - configuring from resource: /hibernate.cf
g.xml
11:58:27,578 INFO Configuration:1081 - Configuration resource: /hibernate.cfg.x
ml
11:58:27,734 INFO Configuration:444 - Mapping resource: com/merin/Cat.hbm.xml
11:58:27,875 INFO HbmBinder:259 - Mapping class: com.merin.Cat -> CAT
11:58:27,921 INFO Configuration:1222 - Configured SessionFactory: null
11:58:27,921 INFO Configuration:875 - processing extends queue
11:58:27,937 INFO Configuration:879 - processing collection mappings
11:58:27,937 INFO Configuration:888 - processing association property reference
s
11:58:27,937 INFO Configuration:917 - processing foreign key constraints
11:58:28,078 INFO DriverManagerConnectionProvider:41 - Using Hibernate built-in
connection pool (not for production use!)
11:58:28,078 INFO DriverManagerConnectionProvider:42 - Hibernate connection poo
l size: 12
11:58:28,078 INFO DriverManagerConnectionProvider:45 - autocommit mode: false
11:58:28,078 INFO DriverManagerConnectionProvider:80 - using driver: com.mysql.
jdbc.Driver at URL: jdbc:mysql:///hib
11:58:28,078 INFO DriverManagerConnectionProvider:86 - connection properties: {
user=root, password=****}
11:58:28,234 INFO SettingsFactory:76 - RDBMS: MySQL, version: 4.0.13-nt
11:58:28,421 INFO SettingsFactory:77 - JDBC driver: MySQL-AB JDBC Driver, versi
on: 3.0.8-stable ( $Date: 2003/05/19 00:57:19 $, $Revision: 1.27.2.18 $ )
11:58:28,453 INFO Dialect:92 - Using dialect: org.hibernate.dialect.MySQLDialec
t
11:58:28,468 INFO TransactionFactoryFactory:31 - Using default transaction stra
tegy (direct JDBC transactions)
11:58:28,484 INFO TransactionManagerLookupFactory:33 - No TransactionManagerLoo
kup configured (in JTA environment, use of read-write or transactional second-le
vel cache is not recommended)
11:58:28,484 INFO SettingsFactory:124 - Automatic flush during beforeCompletion
(): disabled
11:58:28,484 INFO SettingsFactory:128 - Automatic session close at end of trans
action: disabled
11:58:28,484 INFO SettingsFactory:135 - JDBC batch size: 15
11:58:28,500 INFO SettingsFactory:138 - JDBC batch updates for versioned data:
enabled
11:58:28,500 INFO SettingsFactory:143 - Scrollable result sets: enabled
11:58:28,500 INFO SettingsFactory:151 - JDBC3 getGeneratedKeys(): enabled
11:58:28,500 INFO SettingsFactory:159 - Connection release mode: null
11:58:28,515 INFO SettingsFactory:183 - Maximum outer join fetch depth: 1
11:58:28,515 INFO SettingsFactory:186 - Default batch fetch size: 1
11:58:28,515 INFO SettingsFactory:190 - Generate SQL with comments: disabled
11:58:28,515 INFO SettingsFactory:194 - Order SQL updates by primary key: disab
led
11:58:28,515 INFO SettingsFactory:319 - Query translator: org.hibernate.hql.ast
.ASTQueryTranslatorFactory
11:58:28,531 INFO ASTQueryTranslatorFactory:21 - Using ASTQueryTranslatorFactor
y
11:58:28,531 INFO SettingsFactory:202 - Query language substitutions: {no='N',
true=1, yes='Y', false=0}
11:58:28,531 INFO SettingsFactory:208 - Second-level cache: enabled
11:58:28,531 INFO SettingsFactory:212 - Query cache: disabled
11:58:28,546 INFO SettingsFactory:306 - Cache provider: org.hibernate.cache.Has
htableCacheProvider
11:58:28,546 INFO SettingsFactory:227 - Optimize cache for minimal puts: disabl
ed
11:58:28,546 INFO SettingsFactory:232 - Cache region prefix: hibernate.test
11:58:28,546 INFO SettingsFactory:236 - Structured second-level cache entries:
disabled
11:58:28,578 INFO SettingsFactory:256 - Echoing all SQL to stdout
11:58:28,578 INFO SettingsFactory:260 - Statistics: disabled
11:58:28,578 INFO SettingsFactory:264 - Deleted entity synthetic identifier rol
lback: disabled
11:58:28,578 INFO SettingsFactory:278 - Default entity-mode: pojo
11:58:28,796 INFO SessionFactoryImpl:152 - building session factory
11:58:29,375 INFO SessionFactoryObjectFactory:82 - Not binding factory to JNDI,
no JNDI name configured
11:58:29,375 INFO SessionFactoryImpl:379 - Checking 0 named queries
PROBLEM 2
Then I tried to create table using Automatic schema generation.
Executing the build.bat gave the following result:
E:\HIBERNATE\hibernate-3.0.4\hibernate-3.0>build.bat &
E:\HIBERNATE\hibernate-3.0.4\hibernate-3.0>set JDBC_DRIVER=jdbc/hsqldb.jar
E:\HIBERNATE\hibernate-3.0.4\hibernate-3.0>java -cp "lib/ant-launcher-1.6.3.jar"
org.apache.tools.ant.launch.Launcher -lib lib -Ddriver.jar=jdbc/hsqldb.jar
Buildfile: build.xml
schemaexport:
[schemaexport] 12:07:46,109 INFO Environment:464 - Hibernate 3.0.4
[schemaexport] 12:07:46,125 INFO Environment:482 - loaded properties from resou
rce hibernate.properties: {hibernate.connection.driver_class=com.mysql.jdbc.Driv
er, hibernate.cglib.use_reflection_optimizer=true, hibernate.cache.provider_clas
s=org.hibernate.cache.HashtableCacheProvider, hibernate.max_fetch_depth=1, hiber
nate.dialect=org.hibernate.dialect.MySQLDialect, hibernate.jdbc.use_streams_for_
binary=true, hibernate.query.substitutions=true 1, false 0, yes 'Y', no 'N', hib
ernate.connection.username=root, hibernate.cache.region_prefix=hibernate.test, h
ibernate.connection.url=jdbc:mysql:///hib, hibernate.show_sql=true, hibernate.co
nnection.password=****, hibernate.jdbc.batch_versioned_data=true, hibernate.conn
ection.pool_size=1}
[schemaexport] 12:07:46,140 INFO Environment:509 - using java.io streams to per
sist binary types
[schemaexport] 12:07:46,140 INFO Environment:510 - using CGLIB reflection optim
izer
[schemaexport] 12:07:46,140 INFO Environment:540 - using JDK 1.4 java.sql.Times
tamp handling
[schemaexport] 12:07:46,312 INFO Configuration:223 - Mapping file: E:\HIBERNATE
\hibernate-3.0.4\hibernate-3.0\eg\org\hibernate\auction\AuctionItem.hbm.xml
[schemaexport] 12:07:46,578 INFO HbmBinder:259 - Mapping class: org.hibernate.a
uction.AuctionItem -> AuctionItem
[schemaexport] 12:07:46,593 ERROR Configuration:365 - Could not compile the mapp
ing document
[schemaexport] org.hibernate.MappingException: class org.hibernate.auction.Aucti
onItem not found while looking for property: id
[schemaexport] at org.hibernate.util.ReflectHelper.reflectedPropertyClass(Refle
ctHelper.java:81)
[schemaexport] at org.hibernate.mapping.SimpleValue.setTypeUsingReflection(Simp
leValue.java:275)
[schemaexport] at org.hibernate.cfg.HbmBinder.bindSimpleId(HbmBinder.java:359)
[schemaexport] at org.hibernate.cfg.HbmBinder.bindRootPersistentClassCommonValu
es(HbmBinder.java:293)
[schemaexport] at org.hibernate.cfg.HbmBinder.bindRootClass(HbmBinder.java:235)
[schemaexport] at org.hibernate.cfg.HbmBinder.bindRoot(HbmBinder.java:151)
[schemaexport] at org.hibernate.cfg.Configuration.add(Configuration.java:362)
[schemaexport] at org.hibernate.cfg.Configuration.addFile(Configuration.java:22
8)
[schemaexport] at org.hibernate.tool.hbm2ddl.SchemaExportTask.getConfiguration(
SchemaExportTask.java:195)
[schemaexport] at org.hibernate.tool.hbm2ddl.SchemaExportTask.execute(SchemaExp
ortTask.java:135)
[schemaexport] at org.apache.tools.ant.UnknownElement.execute(UnknownElement.ja
va:275)
[schemaexport] at org.apache.tools.ant.Task.perform(Task.java:364)
[schemaexport] at org.apache.tools.ant.Target.execute(Target.java:341)
[schemaexport] at org.apache.tools.ant.Target.performTasks(Target.java:369)
[schemaexport] at org.apache.tools.ant.Project.executeSortedTargets(Project.jav
a:1216)
[schemaexport] at org.apache.tools.ant.Project.executeTarget(Project.java:1185)
[schemaexport] at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(De
faultExecutor.java:40)
[schemaexport] at org.apache.tools.ant.Project.executeTargets(Project.java:1068
)
[schemaexport] at org.apache.tools.ant.Main.runBuild(Main.java:668)
[schemaexport] at org.apache.tools.ant.Main.startAnt(Main.java:187)
[schemaexport] at org.apache.tools.ant.launch.Launcher.run(Launcher.java:246)
[schemaexport] at org.apache.tools.ant.launch.Launcher.main(Launcher.java:67)
[schemaexport] Caused by: java.lang.ClassNotFoundException: org.hibernate.auctio
n.AuctionItem
[schemaexport] at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
[schemaexport] at java.security.AccessController.doPrivileged(Native Method)
[schemaexport] at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
[schemaexport] at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
[schemaexport] at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
[schemaexport] at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
[schemaexport] at java.lang.Class.forName0(Native Method)
[schemaexport] at java.lang.Class.forName(Class.java:164)
[schemaexport] at org.hibernate.util.ReflectHelper.classForName(ReflectHelper.j
ava:108)
[schemaexport] at org.hibernate.util.ReflectHelper.reflectedPropertyClass(Refle
ctHelper.java:77)
[schemaexport] ... 21 more
[schemaexport] 12:07:46,687 ERROR Configuration:232 - Could not configure datast
ore from file: E:\HIBERNATE\hibernate-3.0.4\hibernate-3.0\eg\org\hibernate\aucti
on\AuctionItem.hbm.xml
[schemaexport] org.hibernate.MappingException: class org.hibernate.auction.Aucti
onItem not found while looking for property: id
[schemaexport] at org.hibernate.util.ReflectHelper.reflectedPropertyClass(Refle
ctHelper.java:81)
[schemaexport] at org.hibernate.mapping.SimpleValue.setTypeUsingReflection(Simp
leValue.java:275)
[schemaexport] at org.hibernate.cfg.HbmBinder.bindSimpleId(HbmBinder.java:359)
[schemaexport] at org.hibernate.cfg.HbmBinder.bindRootPersistentClassCommonValu
es(HbmBinder.java:293)
[schemaexport] at org.hibernate.cfg.HbmBinder.bindRootClass(HbmBinder.java:235)
[schemaexport] at org.hibernate.cfg.HbmBinder.bindRoot(HbmBinder.java:151)
[schemaexport] at org.hibernate.cfg.Configuration.add(Configuration.java:362)
[schemaexport] at org.hibernate.cfg.Configuration.addFile(Configuration.java:22
8)
[schemaexport] at org.hibernate.tool.hbm2ddl.SchemaExportTask.getConfiguration(
SchemaExportTask.java:195)
[schemaexport] at org.hibernate.tool.hbm2ddl.SchemaExportTask.execute(SchemaExp
ortTask.java:135)
[schemaexport] at org.apache.tools.ant.UnknownElement.execute(UnknownElement.ja
va:275)
[schemaexport] at org.apache.tools.ant.Task.perform(Task.java:364)
[schemaexport] at org.apache.tools.ant.Target.execute(Target.java:341)
[schemaexport] at org.apache.tools.ant.Target.performTasks(Target.java:369)
[schemaexport] at org.apache.tools.ant.Project.executeSortedTargets(Project.jav
a:1216)
[schemaexport] at org.apache.tools.ant.Project.executeTarget(Project.java:1185)
[schemaexport] at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(De
faultExecutor.java:40)
[schemaexport] at org.apache.tools.ant.Project.executeTargets(Project.java:1068
)
[schemaexport] at org.apache.tools.ant.Main.runBuild(Main.java:668)
[schemaexport] at org.apache.tools.ant.Main.startAnt(Main.java:187)
[schemaexport] at org.apache.tools.ant.launch.Launcher.run(Launcher.java:246)
[schemaexport] at org.apache.tools.ant.launch.Launcher.main(Launcher.java:67)
[schemaexport] Caused by: java.lang.ClassNotFoundException: org.hibernate.auctio
n.AuctionItem
[schemaexport] at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
[schemaexport] at java.security.AccessController.doPrivileged(Native Method)
[schemaexport] at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
[schemaexport] at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
[schemaexport] at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
[schemaexport] at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
[schemaexport] at java.lang.Class.forName0(Native Method)
[schemaexport] at java.lang.Class.forName(Class.java:164)
[schemaexport] at org.hibernate.util.ReflectHelper.classForName(ReflectHelper.j
ava:108)
[schemaexport] at org.hibernate.util.ReflectHelper.reflectedPropertyClass(Refle
ctHelper.java:77)
[schemaexport] ... 21 more
BUILD FAILED
E:\HIBERNATE\hibernate-3.0.4\hibernate-3.0\build.xml:37: Schema text failed: Cou
ld not configure datastore from file: E:\HIBERNATE\hibernate-3.0.4\hibernate-3.0
\eg\org\hibernate\auction\AuctionItem.hbm.xml
Total time: 1 second
I have put the class files but it says that org.hibernate.MappingException: class org.hibernate.auction.AuctionItem not found while looking for property: id
Whats the problem with my table creation? Please give a solution to create mysql tables using hibernate.
Thanks in advance
Merin
|