I am just starting to experiment with Hibernate. I set up a project in eclipse, with no folder hierarchy as to simplify by keeping all the files in the same folder.
The files that Eclipse auto-generated were a bit weird, and didnt work, so I have modified them. I have mysql installed, with a Schema named Test, that contains the table "DOG with attributes IDNR and NAME.
Here are my files:
Dog.java: import java.io.Serializable;
public class Dog implements Serializable{ private Long id; private String name; public Dog(String name){ this.name=name; } public Long getId(){ return id; } public String getName(){ return name; } public void setId(Long id){ this.id=id; } public void setName(String name){ this.name=name; }
}
HiberTest.java:
import org.hibernate.SessionFactory; import org.hibernate.Session; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; //import javax.persistence.Entity; //import org.hibernate.*; //import org.hibernate.annotations.Entity;
public class HiberTest {
/** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory(); Session session = sessionFactory.openSession(); Dog d1 = new Dog("Fido"); System.out.println(d1.getName()); Transaction tx = session.beginTransaction(); session.saveOrUpdate(d1); tx.commit(); session.close(); /*// First unit of work Session session = HibernateUtil.getSessionFactory().openSession(); Transaction tx = session.beginTransaction(); session.save(d1); tx.commit(); session.close();*/
}
}
Dog.hmb.xml: <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Generated Mar 31, 2010 5:46:30 PM by Hibernate Tools 3.3.0.GA --> <hibernate-mapping> <class name="Dog" table="DOG"> <id column="IDNR" name="id" type="java.lang.Long"> <generator class="sequence" /> </id> <property column="NAME" length="30" name="name" type="java.lang.String"/> </property> </class> </hibernate-mapping>
hibernate.cfg.xml: <?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="show_sql">true</property> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.password">rockaren</property> <property name="hibernate.connection.url">jdbc:mysql://localhost/Test</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> </session-factory>
</hibernate-configuration>
When I run the HiberTest file, I get:
83 [main] INFO org.hibernate.cfg.Environment - Hibernate 3.5.0-CR-2 86 [main] INFO org.hibernate.cfg.Environment - hibernate.properties not found 94 [main] INFO org.hibernate.cfg.Environment - Bytecode provider name : javassist 117 [main] INFO org.hibernate.cfg.Environment - using JDK 1.4 java.sql.Timestamp handling 332 [main] INFO org.hibernate.cfg.Configuration - configuring from resource: /hibernate.cfg.xml 332 [main] INFO org.hibernate.cfg.Configuration - Configuration resource: /hibernate.cfg.xml 460 [main] INFO org.hibernate.cfg.Configuration - Configured SessionFactory: null 468 [main] INFO org.hibernate.connection.DriverManagerConnectionProvider - Using Hibernate built-in connection pool (not for production use!) 469 [main] INFO org.hibernate.connection.DriverManagerConnectionProvider - Hibernate connection pool size: 20 469 [main] INFO org.hibernate.connection.DriverManagerConnectionProvider - autocommit mode: false 479 [main] INFO org.hibernate.connection.DriverManagerConnectionProvider - using driver: com.mysql.jdbc.Driver at URL: jdbc:mysql://localhost/Test 479 [main] INFO org.hibernate.connection.DriverManagerConnectionProvider - connection properties: {user=root, password=****} 902 [main] INFO org.hibernate.cfg.SettingsFactory - RDBMS: MySQL, version: 5.1.37-1ubuntu5.1 902 [main] INFO org.hibernate.cfg.SettingsFactory - JDBC driver: MySQL-AB JDBC Driver, version: mysql-connector-java-5.1.12 ( Revision: ${bzr.revision-id} ) 942 [main] INFO org.hibernate.dialect.Dialect - Using dialect: org.hibernate.dialect.MySQLDialect 1011 [main] INFO org.hibernate.engine.jdbc.JdbcSupportLoader - Disabling contextual LOB creation as JDBC driver reported JDBC version [3] less than 4 1014 [main] INFO org.hibernate.transaction.TransactionFactoryFactory - Using default transaction strategy (direct JDBC transactions) 1017 [main] INFO org.hibernate.transaction.TransactionManagerLookupFactory - No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended) 1017 [main] INFO org.hibernate.cfg.SettingsFactory - Automatic flush during beforeCompletion(): disabled 1017 [main] INFO org.hibernate.cfg.SettingsFactory - Automatic session close at end of transaction: disabled 1018 [main] INFO org.hibernate.cfg.SettingsFactory - JDBC batch size: 15 1018 [main] INFO org.hibernate.cfg.SettingsFactory - JDBC batch updates for versioned data: disabled 1029 [main] INFO org.hibernate.cfg.SettingsFactory - Scrollable result sets: enabled 1029 [main] INFO org.hibernate.cfg.SettingsFactory - JDBC3 getGeneratedKeys(): enabled 1029 [main] INFO org.hibernate.cfg.SettingsFactory - Connection release mode: auto 1031 [main] INFO org.hibernate.cfg.SettingsFactory - Maximum outer join fetch depth: 2 1031 [main] INFO org.hibernate.cfg.SettingsFactory - Default batch fetch size: 1 1031 [main] INFO org.hibernate.cfg.SettingsFactory - Generate SQL with comments: disabled 1031 [main] INFO org.hibernate.cfg.SettingsFactory - Order SQL updates by primary key: disabled 1031 [main] INFO org.hibernate.cfg.SettingsFactory - Order SQL inserts for batching: disabled 1031 [main] INFO org.hibernate.cfg.SettingsFactory - Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory 1034 [main] INFO org.hibernate.hql.ast.ASTQueryTranslatorFactory - Using ASTQueryTranslatorFactory 1034 [main] INFO org.hibernate.cfg.SettingsFactory - Query language substitutions: {} 1034 [main] INFO org.hibernate.cfg.SettingsFactory - JPA-QL strict compliance: disabled 1034 [main] INFO org.hibernate.cfg.SettingsFactory - Second-level cache: enabled 1034 [main] INFO org.hibernate.cfg.SettingsFactory - Query cache: disabled 1034 [main] INFO org.hibernate.cfg.SettingsFactory - Cache region factory : org.hibernate.cache.impl.NoCachingRegionFactory 1034 [main] INFO org.hibernate.cfg.SettingsFactory - Optimize cache for minimal puts: disabled 1034 [main] INFO org.hibernate.cfg.SettingsFactory - Structured second-level cache entries: disabled 1040 [main] INFO org.hibernate.cfg.SettingsFactory - Echoing all SQL to stdout 1041 [main] INFO org.hibernate.cfg.SettingsFactory - Statistics: disabled 1041 [main] INFO org.hibernate.cfg.SettingsFactory - Deleted entity synthetic identifier rollback: disabled 1041 [main] INFO org.hibernate.cfg.SettingsFactory - Default entity-mode: pojo 1042 [main] INFO org.hibernate.cfg.SettingsFactory - Named query checking : enabled 1042 [main] INFO org.hibernate.cfg.SettingsFactory - Check Nullability in Core (should be disabled when Bean Validation is on): enabled 1135 [main] INFO org.hibernate.impl.SessionFactoryImpl - building session factory 1142 [main] INFO org.hibernate.impl.SessionFactoryObjectFactory - Not binding factory to JNDI, no JNDI name configured FidoException in thread "main" org.hibernate.MappingException: Unknown entity: Dog at org.hibernate.impl.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:678) at org.hibernate.impl.SessionImpl.getEntityPersister(SessionImpl.java:1475) at org.hibernate.engine.ForeignKeys.isTransient(ForeignKeys.java:203) at org.hibernate.event.def.AbstractSaveEventListener.getEntityState(AbstractSaveEventListener.java:532) at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:103) at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93) at org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:677) at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:669) at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:665) at HiberTest.main(HiberTest.java:28)
What is wrong? Ive sat for three days trying to figure this out! I really do apologize, truly, I understand I must be making some newbie mistake that really shouldnt be hard to find out or google, but I have done so and still not found out what Im doing wrong. I guess I am stupid.
|