嗨傢伙 我建立一個簡單的ToDoList(這只是我的項目的開始),應該添加任務到我的日程安排通過使用MAMP作爲數據庫。 我的問題是season.save()缺少查詢中的主鍵(「id」); 我有以下代碼: 主類:休眠session.save()缺少查詢
package Model;
import java.util.Date;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
//create exception instead of HibernateException ToDoListDAOException
public class HibernateToDoListDAO implements IToDoListDAO {
SessionFactory factory = null;
private static final HibernateToDoListDAO _instanceDAO = new HibernateToDoListDAO();
// no need to do connection or use driver to my sql
private HibernateToDoListDAO(){
factory = new AnnotationConfiguration().configure().buildSessionFactory();
}
public static HibernateToDoListDAO getInstance()
{
return _instanceDAO;
}
@Override
public void addMemorandums(items memorandum) throws HibernateException {
Session session = factory.openSession();
try{
session.beginTransaction();
//create the query for memorandum members
session.saveOrUpdate(memorandum);
System.out.println("after save : user id's = "+memorandum.getId());
session.getTransaction().commit();
}catch(HibernateException e)
{
e.printStackTrace();
if (session.getTransaction() != null)
session.getTransaction().rollback();
}finally{
session.close();
}
}
@Override
public void updateMemorandums(items memorandum) throws HibernateException {
Session session = null;
try{
session = factory.openSession();
session.beginTransaction();
session.update(memorandum);
session.getTransaction().commit();
}catch(HibernateException e)
{
e.printStackTrace();
if (session.getTransaction() != null)
session.getTransaction().rollback();
}finally{
session.close();
}
}
@Override
public void deleteMemorandums(items memorandum) throws HibernateException {
Session session = null;
try{
session = factory.openSession();
session.beginTransaction();
session.delete(memorandum);
session.getTransaction().commit();
}catch(HibernateException e)
{
e.printStackTrace();
if (session.getTransaction() != null)
session.getTransaction().rollback();
}finally{
session.close();
}
}
@SuppressWarnings("unchecked")
@Override
public List<items> getMemorandums() throws HibernateException {
List<items> memorandum = null;
Session session = null;
try{
session = factory.openSession();
session.beginTransaction();
memorandum = session.createQuery("from todolists").list();
}catch(HibernateException e)
{
e.printStackTrace();
if (session.getTransaction() != null)
session.getTransaction().rollback();
}finally{
session.close();
}
return memorandum;
}
public static void main(String[] args) {
@SuppressWarnings("deprecation")
String dateTask = "26/11/2016";
items task1 = new items(2012,"football", dateTask , "ball");
HibernateToDoListDAO.getInstance().addMemorandums(task1);
}
}
項目類:
package Model;
import java.util.Date;
public class items {
private int id;// primary key that connect to the users
private String taskname;
private String date;
private String note;
public items(){
taskname = "basketball";
note = "a fun game";
date = "15/10/2016";
id = 2012;
}
public items(int id, String taskName, String taskDate, String note) {
super();
this.taskname = taskName;
this.date = taskDate;
this.note = note;
this.id = id;
}
public String getTaskname() {
return taskname;
}
public void setTaskname(String taskname) {
this.taskname = taskname;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getNote() {
return note;
}
public void setNote(String note) {
this.note = note;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
我試圖運行這個簡單的程序,我得到那個看起來異常,如:
(例外)
Hibernate: insert into items (taskname, date, note) values (?, ?, ?)
org.hibernate.HibernateException: The database returned no natively generated identity value
at org.hibernate.id.IdentifierGeneratorHelper.getGeneratedIdentity(IdentifierGeneratorHelper.java:84)
at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:98)
at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:57)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2329)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2836)
at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:71)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:267)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:321)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:204)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:130)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:117)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)
at org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:677)
at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:669)
at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:665)
at Model.HibernateToDoListDAO.addMemorandums(HibernateToDoListDAO.java:36)
at Model.HibernateToDoListDAO.main(HibernateToDoListDAO.java:111)
hibernate.cfg.xml中:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="hibernate.connection.driver_class">
com.mysql.jdbc.Driver
</property>
<!-- Assume test is the database name -->
<property name="hibernate.connection.url">
jdbc:mysql://localhost:3306/hit
</property>
<property name="hibernate.connection.username">hit</property>
<property name="hibernate.connection.password">hit</property>
<property name="show_sql">true</property>
<!-- Mapping files -->
<mapping resource="hibernate.mapping.xml"/>
</session-factory>
</hibernate-configuration>
hibernate.mapping.xml:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<!-- *********** items table ******* -->
<!-- full class name including the packege name -->
<class name="Model.items" table="items">
<meta attribute="class-description">
This class contains the the items details.
</meta>
<id name="id" type="int" column="id"> <!-- primary key -->
<generator class="native"/>
</id>
<property name="taskname" column="taskname" type="string"/>
<property name="date" column="date" type="string"/>
<property name="note" column="note" type="string"/>
</class>
<!-- *********** users table ******* -->
<!-- full class name including the packege name -->
<class name="Model.users" table="users">
<meta attribute="class-description">
This class contains the users details.
</meta>
<id name="id" type="int" column="id">
<generator class="native"/>
</id>
<property name="name" column="name" type="string"/>
<property name="age" column="age" type="int"/>
</class>
</hibernate-mapping>
我使用甲基苯丙胺作爲我的數據庫(MySQL的工作)。 問題是,查詢只插入三個而不是四個變量(缺少「id」成員)。 有人可以幫我解決這個問題嗎?
謝謝你們。
你需要'id'列作爲'自動增量'在你的分貝。正如您正在學習的,現在是熟悉java命名標準的好時機,以下鏈接http://www.oracle.com/technetwork/java/codeconventions-135099.html –