Hibernate入門

來源:互聯網
上載者:User

標籤:config   setting   mode   tle   rate   pen   cti   configure   問題   

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列自動成長或在資料庫中不手動建立該張資料表

Hibernate入門

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.