以下是在hibernate 3.2中运行的代码
public class Hibernate3Session {
public static void main(String[] args) throws Exception{
Configuration configuration = new Configuration().configure("hibernate.cfg.xml");
sessionFactory = configuration.buildSessionFactory();
Session session = sessionFactory.openSession();
org.hibernate.Transaction tr = session.beginTransaction();
Employee emp = new Employee();
emp.setName("Employee1");
emp.setContactNumber("111111");
emp.setAddress("XYZ location");
session.save(emp);
tr.commit();
session.close();
System.exit(0);
}
}
以下是员工表
CREATE TABLE Employee
(
ID NUMBER(10) NOT NULL,
NAME VARCHAR2(50 CHAR) NOT NULL,
CONTACT_NUMBER VARCHAR2(50 CHAR),
ADDRESS VARCHAR2(50 CHAR),
PRIMARY KEY (ID)
);
and the sequences used for the same
CREATE SEQUENCE SEQ_EMPLOYEE_ID
MINVALUE 1
NOMAXVALUE
INCREMENT BY 1 START WITH 1 NOCACHE NOCYCLE ;
下面是hibernate.cfg.xml文件
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">oracle.jdbc.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:dbname</property>
<property name="connection.username">SYSTEM</property>
<property name="connection.password">systempassword</property>
<property name="dialect">org.hibernate.dialect.OracleDialect</property>
<property name="hibernate.connection.autocommit">true</property>
<property name="show_sql">true</property>
<mapping resource="Employee.hbm.xml" />
</session-factory>
</hibernate-configuration>
以下是相应的Employee.hbm.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">
<hibernate-mapping>
<class name="Employee" table="EMPLOYEE" >
<id name="id" type="long" >
<column name="ID" />
<generator class="sequence">
<param name="sequence">SEQ_EMPLOYEE_ID</param>
</generator>
</id>
<property name="name" type="string">
<column name="NAME" length="50" not-null="true" />
</property>
<property name="contactNumber" type="string">
<column name="CONTACT_NUMBER" length="50" />
</property>
<property name="address" type="string">
<column name="ADDRESS" length="50" />
</property>
</class>
</hibernate-mapping>
以下是相同的POJO类
public class Employee {
private Long id;
private String name;
private String contactNumber;
private String address;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getContactNumber() {
return contactNumber;
}
public void setContactNumber(String contactNumber) {
this.contactNumber = contactNumber;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((address == null) ? 0 : address.hashCode());
result = prime * result + ((contactNumber == null) ? 0 : contactNumber.hashCode());
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Employee other = (Employee) obj;
if (address == null) {
if (other.address != null)
return false;
} else if (!address.equals(other.address))
return false;
if (contactNumber == null) {
if (other.contactNumber != null)
return false;
} else if (!contactNumber.equals(other.contactNumber))
return false;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
}
用于hibernate 5.2最终版本的代码如下
public class Hibernate5vSession {
public static void main(String[] args) throws Exception{
SessionFactory sessionFactory = null;
StandardServiceRegistryBuilder registryBuilder = new StandardServiceRegistryBuilder().configure("hibernate.cfg.xml");
MetadataSources sources = new MetadataSources(registryBuilder.build());
//sources.addResource("EmployeeRegistration.hbm.xml");
Metadata metaData = sources.getMetadataBuilder().build();
sessionFactory = metaData.getSessionFactoryBuilder().build();
Session session = sessionFactory.openSession();
org.hibernate.Transaction tr = session.beginTransaction();
Employee emp = new Employee();
emp.setName("Employee1");
emp.setContactNumber("111111");
emp.setAddress("XYZ location");
session.save(emp);
tr.commit();
session.close();
System.exit(0);
}
}
还改变了下面的方言:
<property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
获得以下错误
Hibernate:从dual中选择hibernate_sequence.nextval Mar 06,20188 7:56:02 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions 警告:SQL错误:2289,SQLState:42000 Mar 06,20188 7:56:02 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions 错误:ORA-02289:序列不存在
Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not extract ResultSet
at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:106)
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:111)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:97)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:69)
at org.hibernate.id.enhanced.SequenceStructure$1.getNextValue(SequenceStructure.java:95)
at org.hibernate.id.enhanced.NoopOptimizer.generate(NoopOptimizer.java:40)
at org.hibernate.id.enhanced.SequenceStyleGenerator.generate(SequenceStyleGenerator.java:452)
at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:105)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:192)
at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:38)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:177)
at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:32)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:73)
at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:691)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:683)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:678)
at HibernateSessionFactory.main(HibernateSessionFactory.java:62)
Caused by: java.sql.SQLException: ORA-02289: sequence does not exist
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:125)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:305)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:272)
at oracle.jdbc.driver.T4C8Oall.receive(T4C8Oall.java:626)
at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:185)
at oracle.jdbc.driver.T4CPreparedStatement.execute_for_describe(T4CPreparedStatement.java:503)
at oracle.jdbc.driver.OracleStatement.execute_maybe_describe(OracleStatement.java:951)
at oracle.jdbc.driver.T4CPreparedStatement.execute_maybe_describe(T4CPreparedStatement.java:535)
at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1046)
at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:2905)
at oracle.jdbc.driver.OraclePreparedStatement.executeQuery(OraclePreparedStatement.java:2946)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:60)
... 13 more
注意:已验证Oracle的权限。该表存在于管理员用户系统架构中,并且用于凭据。
我已经获得了here提到的SessionFactory。
在控制台中,它看起来是从休眠序列而不是Oracle
Hibernate: select hibernate_sequence.nextval from dual
Mar 06, 2018 10:26:47 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN: SQL Error: 2289, SQLState: 42000
为什么在将hibernate版本升级到5.2.12
时这不起作用对此有何帮助?。