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问题的更多相关文章

  1. Execption:the database returned no natively generated identity value

    org.hibernate.HibernateException: The database returned no natively generated identity value at org. ...

  2. The database returned no natively generated identity value错误解决方案

    原因:hibernate项目中在学生表的配置文件中: <id name="studentno" column="studentno"> <ge ...

  3. Database returned no natively generated

    database returned no natively generated 分类:Hibernatehbm.xml中的配置如下: <id name="logId" typ ...

  4. 【转】Hibernate 常见异常

    转载地址:http://smartan.iteye.com/blog/1542137 Hibernate 常见异常net.sf.hibernate.MappingException        当出 ...

  5. mysql 定义自增

    The database returned no natively generated identity value问题 alter table user_table MODIFY user_id I ...

  6. Hibernate 常见异常

    Hibernate 常见异常net.sf.hibernate.MappingException        当出现net.sf.hibernate.MappingException: Error r ...

  7. org.springframework.orm.hibernate3.HibernateSystemException:

    org.springframework.orm.hibernate3.HibernateSystemException: The database returned no natively gener ...

  8. Hibernate自增列保存失败的问题

    author: hiu 更正说明:今天(2014-07-07)才发现的问题,我把@Id设置在了实体类中的id中,@Id是主键,应该设置在实体类的keyjobno中,之前发的文章可能误导了大家,如今更正 ...

  9. 使用 Hibernate 和 MySQL 需要知道的五件事

    https://www.thoughts-on-java.org/5-things-you-need-to-know-when-using-hibernate-with-mysql/ 作者:Thorb ...

随机推荐

  1. ACM-ICPC World Finals 2019 G.First of Her Name

    题意:给一颗字典树,m次查询,每次给出一个字符串,问你该字符串是字典树上多少串的后缀 题解:字典树求广义sam,每次把查询串在sam上跑一遍,最后到达的点的sz就是答案,中途没法走了,就是没有出现过 ...

  2. Oracle PL/SQL语言函数、匿名语句及循环

    一.自定义函数 格式: create or replace function 函数名(参数名 参数类型...) return  返回值类型 as xx vachar2(20)              ...

  3. CRS添加、删除节点

    一.如何删除一个节点: 1.1 在存活的节点上操作,使用dbca删除DB实例 1.2 清除ASM实例  在存活的节点上做如下操作  1.2.1 停止ASM实例    export ORACLE_HOM ...

  4. new 和 delete

    new 和 delete 众所周知,C中的malloc和free是用来申请和释放内存的,相应的C++中也有对应的申请和释放内存的操作,即是new和delete,但是C++的new和delete比C中的 ...

  5. redisObject

    typedef struct redisObject {    unsigned type:4;    unsigned encoding:4;    unsigned lru:REDIS_LRU_B ...

  6. memory prefix il ir im in out 3 i

    1● il   2● ir 不 非 无 :使 ~ 成为:   3● im 4● in 不 非 无 :向内,进入  

  7. python 转换代码格式

    import os dirname="C:\\Users\\haier\\Desktop\\new" def walk(path): for item in os.listdir( ...

  8. ubuntu16.10安装搜狗输入法

    一.搜狗输入法安装 1.首先到搜狗输入法官网下载搜狗输入法,下载的是个deb文件. 搜狗输入法Linux版下载地址:http://pinyin.sogou.com/linux/?r=pinyin 2. ...

  9. 002-linux——控制台的使用:

    1.桌面控制台: 2.字符控制台: .默认6个字符控制台. .独立运行 互不影响 .多用户  多任务 tty-控制台的使用: .开始进入的是图形图面:tty1 就是图形界面. .图形界面切换到字符界面 ...

  10. vs2017 Mariadb/mysql之旅

    记录vs2017使用 ef6+mysql的开发 填坑之旅.我的环境 vm+centos7+ docker-ce+mariadb+vs2017 总的原则是MySql.Data.Entity 要和 mys ...