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 ...
随机推荐
- 轻量级RPC
①自定义一个协议接口继承VersionedProtocol ②自定义协议类实现上面的接口,完善功能需求 ③服务端 ④客户端 二:模拟一个namenode
- 解决spring-boot启动异常Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean
第一种: 需要在主类头加上 @EnableAutoConfiguration 第二种: pom文件是否加了 <dependency> <groupId>org.mybatis ...
- for + setTimeout
一.背景 最近在翻看以前的老书<node.js开发指南>,恰好碰到 for 循环 + setTimeout 的经典例子,于是重新梳理了思路并记录下. 二.写在前面,setTimeout 和 ...
- Spring Boot 打war包并利用docBase指定根目录为打包的工程
指定根目录有两种方式 1:直接将打的war包名称定义为ROOT 2:利用docBase 比如笔者war包名为xibu.war,将该war包丢到/Users/archerlj/Library/apach ...
- nodejs--路径问题
在读写模块中,需要引入读写文件,此时需要注意路径问题.Node.js中为我们提供了两个参数:__dirname和__filename. __dirname:全局变量,存储的是文件所在的文件目录 __f ...
- 【LeetCode】字符串匹配
给定目标串 haystack 和模式串 needle ,返回 needle 在 haystack 中第一次出现的位置下标,若 needle 不是 haystack 的子串则返回 -1. 1. Brut ...
- 十八、Spring框架(AOP)
一.AOP(基于XML方式配置AOP) AOP(Aspect Oriented Program):面向切面编程.思想是:把功能分为核心业务功能和周边功能. 所谓核心业务功能:比如登录,增删改数据都叫做 ...
- jeasyUI DataGrid 根据屏幕宽度自适应, 改变右侧滚动条Size
PC浏览器的Datagrid可以显示多几列,但是在手机浏览器时,只能有选择性的显示前几列. $(window).resize(function () { if (document.body.clien ...
- pre强制 自动换行
转自:http://www.16sucai.com/2010/10/941.html <pre> 元素可定义预格式化的文本.被包围在 pre 元素中的文本通常会保留空格和换行符.而文本也会 ...
- 【配置】pom.xml的配置
pom.xml的配置: 地址:https://mvnrepository.com/ 示例:配置log4j 1.在搜索框中搜索log4j 2.在搜索结果页点击log4j 3.选择一个最新的版本,点击 4 ...