运行环境:
Tomcat 8;
Myeclipse 2014;
Hibernate 4.1
我使用了一个Hibernate操作MySQL的例子来改,报错:
org.hibernate.dialect.Oracle10gDialect does not support identity key generation
找到一个解决方式:http://stackoverflow.com/questions/24009042/org-hibernate-dialect-oracledialect-does-not-support-identity-key-generation
改了可以用,答案说Hibernate操作数据库需要一个sequence,看了不是很明白,由于我需要id自增,所以继续搜。
看到了这个:http://bbs.csdn.net/topics/310131608
原来:oracle不支持identity,支持的是sequence
主键自增配置参考:http://www.cnblogs.com/myfjd/archive/2012/05/08/2489714.html
于是修改文件News.java,我的工程结构(HibernateSessionFactory.java这个文件是多余的):
Newsmanager.java:
package lee;
import org.crazyit.app.domain.News;
import org.hibernate.*;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
/**
* Description:
* <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
* <br/>Copyright (C), 2001-2016, Yeeku.H.Lee
* <br/>This program is protected by copyright laws.
* <br/>Program Name:
* <br/>Date:
* @author Yeeku.H.Lee kongyeeku@163.com
* @version 1.0
*/
public class NewsManager
{
public static void main(String[] args)
throws Exception
{
// 实例化Configuration,
Configuration conf = new Configuration()
// 不带参数的configure()方法默认加载hibernate.cfg.xml文件,
// 如果传入abc.xml作为参数,则不再加载hibernate.cfg.xml,改为加载abc.xml
.configure();
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()
.applySettings(conf.getProperties()).buildServiceRegistry();
// 以Configuration实例创建SessionFactory实例
SessionFactory sf = conf.buildSessionFactory(serviceRegistry);
// 创建Session
Session sess = sf.openSession();
// 开始事务
Transaction tx = sess.beginTransaction();
// 创建消息对象
News n = new News();
// 设置消息标题和消息内容
n.setId(1);
n.setTitle("I love you very much");
n.setContent("xiaojun test,"
+ "网站地址http://www.crazyit.org");
// 保存消息
sess.save(n);
// 提交事务
tx.commit();
// 关闭Session
sess.close();
sf.close();
}
}
News.java:
package org.crazyit.app.domain;
import javax.persistence.*;
/**
* Description:
* <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
* <br/>Copyright (C), 2001-2016, Yeeku.H.Lee
* <br/>This program is protected by copyright laws.
* <br/>Program Name:
* <br/>Date:
* @author Yeeku.H.Lee kongyeeku@163.com
* @version 1.0
*/
@Entity
@Table(name="news_inf")
public class News
{
// 消息类的标识属性,配置id自增
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="SEQ_STORE")
@SequenceGenerator(name="SEQ_STORE", sequenceName="S_ORACLE",allocationSize = 1)
@Column(name = "ID", unique = true, nullable = false, precision = 6, scale = 0)
private Integer id;
// 消息标题
private String title;
// 消息内容
private String content;
// id的setter和getter方法
public void setId(Integer id)
{
this.id = id;
}
public Integer getId()
{
return this.id;
}
// title的setter和getter方法
public void setTitle(String title)
{
this.title = title;
}
public String getTitle()
{
return this.title;
}
// content的setter和getter方法
public void setContent(String content)
{
this.content = content;
}
public String getContent()
{
return this.content;
}
}
Hibernate.cfg.xml配置:
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.connection.password">123456</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
<property name="hibernate.connection.username">scott</property>
<property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
<property name="hbm2ddl.auto">update</property>
<property name="show_sql">true</property>
<mapping class="org.crazyit.app.domain.News"/>
</session-factory>
</hibernate-configuration>
大功告成