The databse returned no natively generated identity value问题
com.cqupt.dayday.model
代码
package com.cqupt.dayday.model;
import java.util.Date; /**
* Created by I am master on 2017/3/1.
*/ public class News {
private Integer id;
private String title;
private String author;
private Date date; public News(String title, String author, Date date) {
this.title = title;
this.author = author;
this.date = date;
} public News() { } public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public String getAuthor() {
return author;
} public void setAuthor(String author) {
this.author = author;
} public String getTitle() {
return title;
} public void setTitle(String title) {
this.title = title;
} public Date getDate() {
return date;
} public void setDate(Date date) {
this.date = date;
} @Override
public String toString() {
return "News{" +
"id=" + id +
", title='" + title + '\'' +
", author='" + author + '\'' +
", date=" + date +
'}';
}
}
配置文件 News.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate Mapping DTD 3.0"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.cqupt.dayday.model.News" table="news">
<id name="id" type="java.lang.Integer">
<column name="id"/>
<!--指定主键的生成方式, native:使用数据库本地方式-->
<generator class="native"/>
</id>
<property name="title" type="java.lang.String">
<column name="title"/>
</property>
<property name="author" type="java.lang.String">
<column name="author"/>
</property>
<property name="date" type="java.util.Date">
<column name="date"/>
</property>
</class>
</hibernate-mapping>
hibernate.cfg.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate Mapping DTD 3.0"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.cqupt.dayday.model.News" table="news">
<id name="id" type="java.lang.Integer">
<column name="id"/>
<!--指定主键的生成方式, native:使用数据库本地方式-->
<generator class="native"/>
</id>
<property name="title" type="java.lang.String">
<column name="title"/>
</property>
<property name="author" type="java.lang.String">
<column name="author"/>
</property>
<property name="date" type="java.util.Date">
<column name="date"/>
</property>
</class>
</hibernate-mapping>
com.cqupt.dayday
测试类
package com.cqupt.dayday; import com.cqupt.dayday.model.News;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.junit.Test; import java.util.Date; /**
* Created by I am master on 2017/3/1.
*/
public class HibernateTest {
@Test
public void test() { SessionFactory sessionFactory=null; Configuration configuration=new Configuration().configure();
//最老的jar包需要这样写,用最新的jar包写会报错
// ServiceRegistry serviceRegistry=new StandardServiceRegistryBuilder()
// .applySettings(configuration.getProperties()).build(); sessionFactory=configuration.buildSessionFactory(); Session session=sessionFactory.openSession(); Transaction transaction=session.beginTransaction(); News news=new News();
news.setAuthor("dayday");
news.setDate(new Date());
news.setTitle("java"); System.out.println(news);
System.out.println("-----"+session); session.save(news); transaction.commit(); session.close(); sessionFactory.close();
} }
遇到的问题
1)The databse returned no natively generated identity value
2)使用老式jar包
出错原因:
在指定主键生成策略的时候、配置了<generator class="native"/> 、这是提供自动增长、为数据表中的主键自动增长、但是如果数据库没有定义id列为自动增长的话、就会出现The database returned no natively generated identity value错误
解决方法:
在数据库中手动定义id列自动增长或在数据库中不手动建立该张数据表
The databse returned no natively generated identity value问题的更多相关文章
- Execption:the database returned no natively generated identity value
org.hibernate.HibernateException: The database returned no natively generated identity value at org. ...
- The database returned no natively generated identity value错误解决方案
原因:hibernate项目中在学生表的配置文件中: <id name="studentno" column="studentno"> <ge ...
- Database returned no natively generated
database returned no natively generated 分类:Hibernatehbm.xml中的配置如下: <id name="logId" typ ...
- 【转】Hibernate 常见异常
转载地址:http://smartan.iteye.com/blog/1542137 Hibernate 常见异常net.sf.hibernate.MappingException 当出 ...
- mysql 定义自增
The database returned no natively generated identity value问题 alter table user_table MODIFY user_id I ...
- Hibernate 常见异常
Hibernate 常见异常net.sf.hibernate.MappingException 当出现net.sf.hibernate.MappingException: Error r ...
- org.springframework.orm.hibernate3.HibernateSystemException:
org.springframework.orm.hibernate3.HibernateSystemException: The database returned no natively gener ...
- Hibernate自增列保存失败的问题
author: hiu 更正说明:今天(2014-07-07)才发现的问题,我把@Id设置在了实体类中的id中,@Id是主键,应该设置在实体类的keyjobno中,之前发的文章可能误导了大家,如今更正 ...
- 使用 Hibernate 和 MySQL 需要知道的五件事
https://www.thoughts-on-java.org/5-things-you-need-to-know-when-using-hibernate-with-mysql/ 作者:Thorb ...
随机推荐
- 【洛谷p5015】标题统计
(写上瘾了再来一篇吧) 标题统计[传送门] 洛谷算法标签 字符串这种东西,我看到是崩溃的.因为我们只学到了二维数组[这个梗自行get],总之我们当时还没有学.然后显然就是各种翻书,各种百度.大致了解了 ...
- csrf漏洞
漏洞原理:csrf全名为跨站请求伪造,是一种对网站的恶意利用,虽然听起来和xss很像,但是它们俩还是有很大的区别的.csrf是通过伪造来自受信任用户的请求来利用受信任的网站. 比如: 一个有csrf漏 ...
- 有一个问题关于stl函数中的算法问题
是不是stl中的算法函数中参数只要是和函数相关的就是函数对象和谓词?
- 导出函数__declspec(dllexport)
一般而言,动态链接库中定义有两种函数:导出函数(export function)和内部函数(internal function). 导出函数可以被其它模块调用,内部函数在定义它们的DLL程序内部使用. ...
- Leetcode 870. 优势洗牌
870. 优势洗牌 显示英文描述 我的提交返回竞赛 用户通过次数49 用户尝试次数92 通过次数49 提交次数192 题目难度Medium 给定两个大小相等的数组 A 和 B,A 相对于 B 的 ...
- HTTP及RFC解析。
HTTP协议描述的是发送方与接收方的通信协议,通过两方的自觉遵守而存在,当然有不少的浏览器并没有百分百遵守这份协议.HTTP是运行于应用层的协议,基于TCP协议而运作.基本上是客户/服务器对答模式,其 ...
- 14. Longest Common Prefix C++
采用纵向遍历,即对第一个字符串,取出第一个字符,检查是否出现在随后每一个字符串中,以此类推.当遍历完成或有一个字符串不符合要求,直接return. class Solution { public: s ...
- express中app.get和app.use的解析
app.get(path,callback)中的callback只能是函数 app.use(path,callback)中的callback既可以是router对象又可以是函数 当其是router对象 ...
- 【转】JavaScript => TypeScript 入门
几个月前把 ES6 的特性都过了一遍,收获颇丰.现在继续来看看 TypesScript(下文简称为 “TS”).限于经验,本文一些总结如有不当,欢迎指正. 官网有这样一段描述: TypeScript ...
- ActiveMQ consumer按顺序处理消息
http://activemq.apache.org/exclusive-consumer.html producer发送消息是有先后顺序的,这种顺序保持到了broker中.如果希望消息按顺序被消费掉 ...