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. linux指令统计日志出现的次数

    cat  XXX.log|grep ''|grep '条件'| wc -l   单个条件统计 cat  XXX.log|grep ''|grep '条件1'|grep '条件2'|grep '条件3' ...

  2. 解决jar包依赖:Spring IO platform推出bom

    名词解释: BOM(bill of materials):材料清单,用于解决jar包依赖的好方法. Spring IO Platform 缘起:Spring起初只专注ioc和aop,现在已发展成一个庞 ...

  3. 牛客网多校第3场Esort string (kmp)

    链接:https://www.nowcoder.com/acm/contest/141/E 来源:牛客网 题目描述 Eddy likes to play with string which is a ...

  4. 音频 API 一览

    iOS 和 OS X 平台都有一系列操作音频的 API,其中涵盖了从低到高的全部层级.随着时间的推移.平台的增长以及改变,不同 API 的数量可以说有着非常巨大的变化.本文对当前可以使用的 API 以 ...

  5. PHP json_encode重要的秘密

    php格式化json的函数 json_encode($value,$options) 其中有2个比较常用到的参数 JSON_UNESCAPED_UNICODE(中文不转为unicode ,对应的数字 ...

  6. zookeeper 的心跳

    假定:主机 A, B 通过 tcp 连接发送数据,如果拔掉 A 主机的网线,B 是无法感知到的.但是如果 A 定时给 B 发送心跳,则能根据心跳的回复来判断连接的状态. 以 zookeeper 为例: ...

  7. RESTful API单元测试(十九)

    下面针对该Controller编写测试用例验证正确性,具体如下.当然也可以通过浏览器插件等进行请求提交验证. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 ...

  8. Javaconfig形式配置Dubbo多注册中心

    多注册中心,一般用不到,但是某些情况下的确能解决不少问题,可以将某些dubbo服务注册到2套dubbo系统中,实现服务在2套系统间的共用. 网上的配置说明很多,但包括dubbo官方说明文档都是以xml ...

  9. 【环境】新建Maven工程步骤及报错解决方法

    新建Maven工程步骤: 1.打开eclipse,File-New-Other-Maven-Maven project 点击Finish,即可创建一个Maven工程.Maven是内置的,不需要额外下载 ...

  10. 逆袭之旅DAY20.xia.师父指导.数据类型

    2018-07-16  09:35:57 基础是王道 从码农--软件工程师--软件架构师 String 首字母大写,特殊引用类型,常量类 二.数组 存钱罐(只能放钱) 数据兼容 数组的长度(定义后长度 ...