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列自动增长或在数据库中不手动建立该张数据表

转载于:https://www.cnblogs.com/Hdaydayup/p/6485197.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值