Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
Hibernate version:
3.0
Mapping documents:
<?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 package="com.mb.purcell.user">
<class name="User" table="user">
<id name="id" column="uid" type="long">
<generator class="native"/>
</id>
<property name="username" type="string" />
<property name="firstname" type="string" />
<property name="lastname" type="string" />
<property name="password" type="string" />
<set name="foods">
<key column="uid" />
<one-to-many class="Food" />
</set>
</class>
</hibernate-mapping>
<?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 package="com.mb.purcell.user">
<class name="Food" table="food">
<id name="id" column="fid" type="long">
<generator class="native"/>
</id>
<property name="name" type="string" />
<property name="address" type="string" />
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():
Not having any issues here
Full stack trace of any exception that occurs:
compile:
[javac] Compiling 1 source file to C:\work\purple3\classes
usertest:
[java] 09:06:57,304 INFO Environment:460 - Hibernate 3.0
[java] 09:06:57,319 INFO Environment:478 - loaded properties from resource hibernate.properties: {hibernate.connection.usern
ame=dev_test, hibernate.connection.password=****, hibernate.cglib.use_reflection_optimizer=false, hibernate.dialect=org.hibernate.
dialect.Oracle9Dialect, hibernate.connection.url=jdbc:oracle:thin:@devweb2.dimensions.com:1521:dev, hibernate.connection.driver_cl
ass=oracle.jdbc.driver.OracleDriver}
[java] 09:06:57,319 INFO Environment:536 - using JDK 1.4 java.sql.Timestamp handling
[java] 09:06:57,413 INFO Configuration:1159 - configuring from resource: /hibernate.cfg.xml
[java] 09:06:57,413 INFO Configuration:1130 - Configuration resource: /hibernate.cfg.xml
[java] 09:06:57,816 INFO Configuration:440 - Mapping resource: com/mb/purcell/Track.hbm.xml
[java] 09:06:57,956 INFO HbmBinder:258 - Mapping class: com.mb.purcell.Track -> TRACK
[java] 09:06:57,987 INFO HbmBinder:1202 - Mapping collection: com.mb.purcell.Track.artists -> TRACK_ARTISTS
[java] 09:06:57,987 INFO Configuration:440 - Mapping resource: com/mb/purcell/Artist.hbm.xml
[java] 09:06:58,049 INFO HbmBinder:258 - Mapping class: com.mb.purcell.Artist -> ARTIST
[java] 09:06:58,049 INFO HbmBinder:1202 - Mapping collection: com.mb.purcell.Artist.tracks -> TRACK_ARTISTS
[java] 09:06:58,049 INFO Configuration:1271 - Configured SessionFactory: null
[java] 09:06:58,065 INFO Configuration:851 - processing extends queue
[java] 09:06:58,065 INFO Configuration:855 - processing collection mappings
[java] 09:06:58,143 INFO Configuration:864 - processing association property references
[java] 09:06:58,143 INFO Configuration:893 - processing foreign key constraints
[java] 09:06:58,174 INFO Dialect:91 - Using dialect: org.hibernate.dialect.MySQLDialect
[java] 09:06:58,189 INFO SettingsFactory:87 - Maximum outer join fetch depth: 2
[java] 09:06:58,189 INFO SettingsFactory:90 - Default batch fetch size: 1
[java] 09:06:58,189 INFO SettingsFactory:94 - Generate SQL with comments: disabled
[java] 09:06:58,189 INFO SettingsFactory:98 - Order SQL updates by primary key: disabled
[java] 09:06:58,189 INFO SettingsFactory:273 - Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
[java] 09:06:58,205 INFO ASTQueryTranslatorFactory:21 - Using ASTQueryTranslatorFactory
[java] 09:06:58,205 INFO SettingsFactory:106 - Query language substitutions: {}
[java] 09:06:58,205 INFO DriverManagerConnectionProvider:41 - Using Hibernate built-in connection pool (not for production u
se!)
[java] 09:06:58,205 INFO DriverManagerConnectionProvider:42 - Hibernate connection pool size: 20
[java] 09:06:58,205 INFO DriverManagerConnectionProvider:45 - autocommit mode: false
[java] 09:06:58,220 INFO DriverManagerConnectionProvider:80 - using driver: com.mysql.jdbc.Driver at URL: jdbc:mysql://local
host/purple3
[java] 09:06:58,220 INFO DriverManagerConnectionProvider:86 - connection properties: {user=root, password=****}
[java] 09:06:58,531 INFO SettingsFactory:148 - JDBC batch size: 15
[java] 09:06:58,531 INFO SettingsFactory:151 - JDBC batch updates for versioned data: disabled
[java] 09:06:58,531 INFO SettingsFactory:156 - Scrollable result sets: enabled
[java] 09:06:58,531 INFO SettingsFactory:164 - JDBC3 getGeneratedKeys(): enabled
[java] 09:06:58,546 INFO TransactionFactoryFactory:31 - Using default transaction strategy (direct JDBC transactions)
[java] 09:06:58,546 INFO TransactionManagerLookupFactory:33 - No TransactionManagerLookup configured (in JTA environment, us
e of read-write or transactional second-level cache is not recommended)
[java] 09:06:58,546 INFO SettingsFactory:176 - Automatic flush during beforeCompletion(): disabled
[java] 09:06:58,546 INFO SettingsFactory:179 - Automatic session close at end of transaction: disabled
[java] 09:06:58,546 INFO SettingsFactory:260 - Cache provider: org.hibernate.cache.EhCacheProvider
[java] 09:06:58,562 INFO SettingsFactory:187 - Second-level cache: enabled
[java] 09:06:58,562 INFO SettingsFactory:192 - Optimize cache for minimal puts: disabled
[java] 09:06:58,562 INFO SettingsFactory:199 - Structured second-level cache entries: enabled
[java] 09:06:58,562 INFO SettingsFactory:203 - Query cache: disabled
[java] 09:06:58,562 INFO SettingsFactory:214 - Statistics: disabled
[java] 09:06:58,562 INFO SettingsFactory:218 - Deleted entity synthetic identifier rollback: disabled
[java] 09:06:58,562 INFO SettingsFactory:232 - Default entity-mode: pojo
[java] 09:06:58,702 INFO SessionFactoryImpl:140 - building session factory
[java] 09:06:58,717 WARN Configurator:126 - No configuration found. Configuring ehcache from ehcache-failsafe.xml found in t
he classpath: jar:file:/C:/work/purple3/lib/ehcache-1.1.jar!/ehcache-failsafe.xml
[java] 09:06:59,183 INFO SessionFactoryObjectFactory:82 - Not binding factory to JNDI, no JNDI name configured
[java] 09:06:59,183 INFO SessionFactoryImpl:366 - Checking 0 named queries
[java] Exception in thread "main" org.hibernate.MappingException: Unknown entity: com.mb.purcell.user.User
[java] at org.hibernate.impl.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:489)
[java] at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:64)
[java] at org.hibernate.impl.SessionImpl.load(SessionImpl.java:549)
[java] at org.hibernate.impl.SessionImpl.load(SessionImpl.java:544)
[java] at com.mb.purcell.user.UserTest.main(UserTest.java:23)
[java] Java Result: 1
BUILD SUCCESSFUL
Total time: 5 seconds
C:\work\purple3>
Name and version of the database you are using:
Mysql version 5
The generated SQL (show_sql=true):
Not getting that far
Debug level Hibernate log excerpt:
debug
Info:
I am trying a very simple starter setup. I have a User object that is an entity on uid.
I also have another entity Food with a primary key of fid. That table has a FK reference to the user uid.
Here is the generated sql.
[schemaexport] create table food (
[schemaexport] fid bigint not null auto_increment,
[schemaexport] name varchar(255),
[schemaexport] address varchar(255),
[schemaexport] uid bigint,
[schemaexport] primary key (fid)
[schemaexport] )
[schemaexport] create table user (
[schemaexport] uid bigint not null auto_increment,
[schemaexport] username varchar(255),
[schemaexport] firstname varchar(255),
[schemaexport] lastname varchar(255),
[schemaexport] password varchar(255),
[schemaexport] primary key (uid)
[schemaexport] )
[schemaexport] alter table food add index FK300C5EB6259573 (uid), add constraint FK300C5EB6259573 foreign key (uid) references
r (uid)
[schemaexport] 3917 [main] INFO hbm2ddl.SchemaExport - schema export complete
[schemaexport] 3917 [main] INFO connection.DriverManagerConnectionProvider - cleaning up connection pool: jdbc:mysql://localho
purple3
Then I manuall inserted 1 record into user. That has a UID of '1'
Than I manually inserted 3 records into food where the FK uid has a 1.
I want to pull back my one user with a set of food items that are there. Seems like a simple thing. I do not understand the MappingException error, as my User class is definitely an entity with a primary key.
I am using hibernate.cfg.xml
<?xml version="1.0"?>
<!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="connection.username">root</property>
<property name="connection.password">admin</property>
<property name="connection.url">
jdbc:mysql://localhost/purple3
</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<mapping resource="com/mb/purcell/Track.hbm.xml" />
<mapping resource="com/mb/purcell/Artist.hbm.xml" />
<mapping resource="com/mb/purcell/user/User.hbm.xml" />
<mapping resource="com/mb/purcell/user/Food.hbm.xml" />
</session-factory>
</hibernate-configuration>
and I am trying to pull data back like so:
public static void main(String[] args) throws Exception {
Configuration config = new Configuration();
config.configure();
SessionFactory sessionFactory = config.buildSessionFactory();
Session session = sessionFactory.openSession();
try {
User usr2 = (User)session.load(User.class, 1);
Thanks