我是Hibernate的新手,我在eclipse luna中使用4.3.6.Final版本。我不知道为什么不能在我的测试中插入记录。我已经根据我的数据库(Oracle 11g Express Edition)生成了POJO类和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 name="">
<property name="hibernate.bytecode.use_reflection_optimizer">false</property>
<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.connection.password">1234</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
<property name="hibernate.connection.username">ELSHOUT_DEV</property>
<property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
<property name="hibernate.search.autoregister_listeners">false</property>
<property name="show_sql">true</property>
<mapping resource="daos/RequiredServices.hbm.xml" />
<mapping resource="daos/CustomerTypes.hbm.xml" />
<mapping resource="daos/Services.hbm.xml" />
<mapping resource="daos/Invoices.hbm.xml" />
<mapping resource="daos/Vehicles.hbm.xml" />
<mapping resource="daos/ServicedVehicles.hbm.xml" />
<mapping resource="daos/Roles.hbm.xml" />
<mapping resource="daos/VehicleConditions.hbm.xml" />
<mapping resource="daos/Users.hbm.xml" />
<mapping resource="daos/Bookings.hbm.xml" />
<mapping resource="daos/Customers.hbm.xml" />
<mapping resource="daos/Activities.hbm.xml" />
<mapping resource="daos/ServiceRatings.hbm.xml" />
<mapping resource="daos/AfterServImages.hbm.xml" />
<mapping resource="daos/BookingStatuses.hbm.xml" />
<mapping resource="daos/BeforeServImages.hbm.xml" />
</session-factory>
</hibernate-configuration>
POJO
package daos;
// Generated 27/10/2015 05:20:35 PM by Hibernate Tools 4.0.0
import java.util.HashSet;
import java.util.Set;
/**
* Activities generated by hbm2java
*/
public class Activities implements java.io.Serializable {
private short activityId;
private String activityName;
private String description;
private String comment;
private Set roleses = new HashSet(0);
public Activities() {
}
public Activities(short activityId, String activityName, String description) {
this.activityId = activityId;
this.activityName = activityName;
this.description = description;
}
public Activities(short activityId, String activityName,
String description, String comment, Set roleses) {
this.activityId = activityId;
this.activityName = activityName;
this.description = description;
this.comment = comment;
this.roleses = roleses;
}
public short getActivityId() {
return this.activityId;
}
public void setActivityId(short activityId) {
this.activityId = activityId;
}
public String getActivityName() {
return this.activityName;
}
public void setActivityName(String activityName) {
this.activityName = activityName;
}
public String getDescription() {
return this.description;
}
public void setDescription(String description) {
this.description = description;
}
public String getComment() {
return this.comment;
}
public void setComment(String comment) {
this.comment = comment;
}
public Set getRoleses() {
return this.roleses;
}
public void setRoleses(Set roleses) {
this.roleses = roleses;
}
}
映射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 27/10/2015 05:20:35 PM by Hibernate Tools 4.0.0 -->
<hibernate-mapping>
<class name="daos.Activities" table="ACTIVITIES" schema="ELSHOUT_DEV">
<id name="activityId" type="short">
<column name="ACTIVITY_ID" precision="4" scale="0" />
<generator class="assigned" />
</id>
<property name="activityName" type="string">
<column name="ACTIVITY_NAME" length="100" not-null="true">
<comment>ACTIVITY NAME</comment>
</column>
</property>
<property name="description" type="string">
<column name="DESCRIPTION" length="500" not-null="true">
<comment>ACTIVITY DESCRIPTION</comment>
</column>
</property>
<property name="comment" type="string">
<column name="COMMENT" length="1000">
<comment>COMMENTS</comment>
</column>
</property>
<set name="roleses" table="ROLES" inverse="true" lazy="true" fetch="select">
<key>
<column name="ACTIVITY_ID" precision="4" scale="0" not-null="true">
<comment>ACTIVITY ID</comment>
</column>
</key>
<one-to-many class="daos.Roles" />
</set>
</class>
</hibernate-mapping>
测试者课程
package dbConnection;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import daos.Activities;
public class TestHibernate {
public TestHibernate()
{
}
public static void main(String[] args)
{
try
{
SessionFactory sessionFactory;
Session session;
Configuration configuration = new Configuration().configure("hibernate.cfg.xml");
StandardServiceRegistryBuilder builder =
new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());
ServiceRegistry serviceRegistry = builder.build();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
Activities activities = new Activities();
activities.setActivityId(new Short("1"));
activities.setActivityName("Wash and Vac");
activities.setDescription("Just wash an quick vacuum");
activities.setComment("Do this very quick");
session = sessionFactory.openSession();
session.getTransaction().begin();
session.save(activities);
session.getTransaction().commit();
session.close();
}
catch (HibernateException e)
{
e.printStackTrace();
System.out.println(e.getMessage());
}
}
}
这是例外追踪:
oct 27, 2015 9:48:42 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.5.Final}
oct 27, 2015 9:48:42 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.3.6.Final}
oct 27, 2015 9:48:42 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
oct 27, 2015 9:48:42 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
oct 27, 2015 9:48:42 PM org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: hibernate.cfg.xml
oct 27, 2015 9:48:42 PM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: hibernate.cfg.xml
oct 27, 2015 9:48:42 PM org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity
WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
oct 27, 2015 9:48:42 PM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: daos/RequiredServices.hbm.xml
oct 27, 2015 9:48:42 PM org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity
WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
oct 27, 2015 9:48:42 PM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: daos/CustomerTypes.hbm.xml
oct 27, 2015 9:48:42 PM org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity
WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
oct 27, 2015 9:48:42 PM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: daos/Services.hbm.xml
oct 27, 2015 9:48:42 PM org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity
WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
oct 27, 2015 9:48:42 PM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: daos/Invoices.hbm.xml
oct 27, 2015 9:48:42 PM org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity
WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
oct 27, 2015 9:48:42 PM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: daos/Vehicles.hbm.xml
oct 27, 2015 9:48:42 PM org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity
WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
oct 27, 2015 9:48:43 PM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: daos/ServicedVehicles.hbm.xml
oct 27, 2015 9:48:43 PM org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity
WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
oct 27, 2015 9:48:43 PM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: daos/Roles.hbm.xml
oct 27, 2015 9:48:43 PM org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity
WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
oct 27, 2015 9:48:43 PM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: daos/VehicleConditions.hbm.xml
oct 27, 2015 9:48:43 PM org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity
WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
oct 27, 2015 9:48:43 PM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: daos/Users.hbm.xml
oct 27, 2015 9:48:43 PM org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity
WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
oct 27, 2015 9:48:43 PM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: daos/Bookings.hbm.xml
oct 27, 2015 9:48:43 PM org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity
WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
oct 27, 2015 9:48:43 PM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: daos/Customers.hbm.xml
oct 27, 2015 9:48:43 PM org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity
WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
oct 27, 2015 9:48:43 PM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: daos/Activities.hbm.xml
oct 27, 2015 9:48:43 PM org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity
WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
oct 27, 2015 9:48:43 PM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: daos/ServiceRatings.hbm.xml
oct 27, 2015 9:48:43 PM org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity
WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
oct 27, 2015 9:48:43 PM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: daos/AfterServImages.hbm.xml
oct 27, 2015 9:48:43 PM org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity
WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
oct 27, 2015 9:48:43 PM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: daos/BookingStatuses.hbm.xml
oct 27, 2015 9:48:43 PM org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity
WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
oct 27, 2015 9:48:43 PM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: daos/BeforeServImages.hbm.xml
oct 27, 2015 9:48:43 PM org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity
WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
oct 27, 2015 9:48:43 PM org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory:
oct 27, 2015 9:48:43 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH000402: Using Hibernate built-in connection pool (not for production use!)
oct 27, 2015 9:48:43 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000401: using driver [oracle.jdbc.driver.OracleDriver] at URL [jdbc:oracle:thin:@localhost:1521:xe]
oct 27, 2015 9:48:43 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000046: Connection properties: {user=ELSHOUT_DEV, password=****}
oct 27, 2015 9:48:43 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000006: Autocommit mode: false
oct 27, 2015 9:48:43 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
oct 27, 2015 9:48:44 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.Oracle10gDialect
oct 27, 2015 9:48:45 PM org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService
INFO: HHH000399: Using default transaction strategy (direct JDBC transactions)
oct 27, 2015 9:48:45 PM org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>
INFO: HHH000397: Using ASTQueryTranslatorFactory
oct 27, 2015 9:48:45 PM org.hibernate.internal.SessionFactoryRegistry addSessionFactory
WARN: HHH000277: Could not bind factory to JNDI
org.hibernate.engine.jndi.JndiException: Error parsing JNDI name []
at org.hibernate.engine.jndi.internal.JndiServiceImpl.parseName(JndiServiceImpl.java:141)
at org.hibernate.engine.jndi.internal.JndiServiceImpl.bind(JndiServiceImpl.java:157)
at org.hibernate.internal.SessionFactoryRegistry.addSessionFactory(SessionFactoryRegistry.java:103)
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:497)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1857)
at dbConnection.TestHibernate.main(TestHibernate.java:30)
Caused by: javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.getNameParser(Unknown Source)
at org.hibernate.engine.jndi.internal.JndiServiceImpl.parseName(JndiServiceImpl.java:135)
... 5 more
Hibernate: insert into ELSHOUT_DEV.ACTIVITIES (ACTIVITY_NAME, DESCRIPTION, COMMENT, ACTIVITY_ID) values (?, ?, ?, ?)
oct 27, 2015 9:48:46 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN: SQL Error: 1747, SQLState: 42000
oct 27, 2015 9:48:46 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: ORA-01747: invalid user.table.column, table.column, or column specification
oct 27, 2015 9:48:46 PM org.hibernate.engine.jdbc.batch.internal.AbstractBatchImpl release
INFO: HHH000010: On release of batch it still contained JDBC statements
could not execute statement
org.hibernate.exception.SQLGrammarException: could not execute statement
at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:80)
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:126)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:112)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:211)
at org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.java:62)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3124)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3581)
at org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.java:104)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:463)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:349)
at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:350)
at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:56)
at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1222)
at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:425)
at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101)
at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:177)
at dbConnection.TestHibernate.main(TestHibernate.java:41)
Caused by: java.sql.SQLSyntaxErrorException: ORA-01747: invalid user.table.column, table.column, or column specification
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:447)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:396)
at oracle.jdbc.driver.T4C8Oall.processError(T4C8Oall.java:951)
at oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:513)
at oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:227)
at oracle.jdbc.driver.T4C8Oall.doOALL(T4C8Oall.java:531)
at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:208)
at oracle.jdbc.driver.T4CPreparedStatement.executeForRows(T4CPreparedStatement.java:1046)
at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1336)
at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:3613)
at oracle.jdbc.driver.OraclePreparedStatement.executeUpdate(OraclePreparedStatement.java:3694)
at oracle.jdbc.driver.OraclePreparedStatementWrapper.executeUpdate(OraclePreparedStatementWrapper.java:1354)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:208)
... 13 more
我将不胜感激任何建议。提前谢谢。