2017-10-08 13 views
0

私のアプリが例外 "フィールド 'Id_employeer'にデフォルト値がない理由を理解できません。フィールド 'Id_employeer'にデフォルト値がありません

Id_employeerが私のプライマリキーです。データベースに情報を保存しようとすると、その例外が発生しますが、DBから情報を読み取ろうとすると機能します。

私はStackOverflowのトピックを読んでいますが、私はまだ私のアプリで答えを理解していません。私はHibernate 5.2.11で作業しています。

マイCharacterInfo.hbm.xml

<?xml version="1.0"?> 

<!-- 
    ~ Hibernate, Relational Persistence for Idiomatic Java 
    ~ 
    ~ License: GNU Lesser General Public License (LGPL), version 2.1 or later. 
    ~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. 
    --> 
<!DOCTYPE hibernate-mapping PUBLIC 
     "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
     "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 

<hibernate-mapping> 

    <class name="CharacterInfo" table="employees"> 
     <id name="Id" column="id_employeer"> 
      <generator class="native"/> 
     </id> 
     <property name="Login" column="employeer_login"/> 



    </class> 

</hibernate-mapping> 

マイHibernateUtility:

import org.hibernate.Session; 
import org.hibernate.SessionFactory; 
import org.hibernate.boot.MetadataSources; 
import org.hibernate.boot.registry.StandardServiceRegistry; 
import org.hibernate.boot.registry.StandardServiceRegistryBuilder; 
import java.util.List; 


public class HibernateUtility { 


    SessionFactory sessionFactory; 

    public void saving(Object object){ 
     Session session = sessionFactory.openSession(); 
     session.beginTransaction(); 
     session.save(object); 
     //session.save(new CharacterInfo().getLogin()); 
     session.getTransaction().commit(); 
     session.close(); 
    } 

    public void reading(){ 
     Session session = sessionFactory.openSession(); 
     session.beginTransaction(); 
     List result = session.createQuery("from CharacterInfo").list(); 
     for (CharacterInfo characterInfo : (List<CharacterInfo>) result) { 
      System.out.println("CharacterInfo (" + characterInfo.getLogin() + ") : " + characterInfo.getLogin()); 
     } 
     session.getTransaction().commit(); 
     session.close(); 
    } 


    protected void setUp() throws Exception { 

     final StandardServiceRegistry registry = new StandardServiceRegistryBuilder().configure() 
       .build(); 

     try { 
      sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory(); 


     } catch (Exception e) { 
      StandardServiceRegistryBuilder.destroy(registry); 
     } 
    } 




} 

次CharacterInfoクラス

import org.hibernate.annotations.GenericGenerator; 

import javax.persistence.*; 


@Entity 
@Table(name = "employees") 
public class CharacterInfo { 

    @Id 
    @GeneratedValue(strategy = GenerationType.AUTO) 
    @Column(name = "Id_employeer") 
    private int id ; 

    @Column(name = "employeer_Login") 
    private String login; 


    public CharacterInfo() {} 

    public int getId() { 
     return id; 

    } 

    public void setId(int id) { 
     this.id = id; 
    } 


    public String getLogin() { 
     return login; 
    } 

    public void setLogin(String login) { 
     this.login = login; 
    } 



} 

メインクラス

public class main { 


    public static void main(String args[]) throws Exception { 
     HibernateUtility hibernateUtility = new HibernateUtility(); 
     CharacterInfo characterInfo = new CharacterInfo(); 
     characterInfo.setId(1); 
     characterInfo.setLogin("PawJaw"); 
     hibernateUtility.setUp(); 
     hibernateUtility.saving(characterInfo); 


    } 
} 

と終了例外で

ERROR: Field 'Id_employeer' doesn't have a default value Exception in thread "main" org.hibernate.exception.GenericJDBCException: could not execute statement at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:47) at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:111) at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:97) at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:178) at org.hibernate.dialect.identity.GetGeneratedKeysDelegate.executeAndExtract(GetGeneratedKeysDelegate.java:57) at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:42) at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2919) at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3490) at org.hibernate.action.internal.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:81) at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:626) at org.hibernate.engine.spi.ActionQueue.addResolvedEntityInsertAction(ActionQueue.java:280) at org.hibernate.engine.spi.ActionQueue.addInsertAction(ActionQueue.java:261) at org.hibernate.engine.spi.ActionQueue.addAction(ActionQueue.java:306) at org.hibernate.event.internal.AbstractSaveEventListener.addInsertAction(AbstractSaveEventListener.java:318) at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:275) at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:182) at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:113) at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:192) at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:38) at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:177) at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:32) at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:73) at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:691) at org.hibernate.internal.SessionImpl.save(SessionImpl.java:683) at org.hibernate.internal.SessionImpl.save(SessionImpl.java:678) at HibernateUtility.saving(HibernateUtility.java:17) at main.main(main.java:10) Caused by: java.sql.SQLException: Field 'Id_employeer' doesn't have a default value at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:964) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3973) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3909) at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2527) at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2680) at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2487) at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1858) at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2079) at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2013) at com.mysql.jdbc.PreparedStatement.executeLargeUpdate(PreparedStatement.java:5104) at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1998) at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:175) ... 23 more

答えて

1

まず第一に、あなたはHibernateのアノテーションを使用している場合、あなたはHibernateのXMLマッピングファイルを使用する必要はありません。

あなたのエラーはおそらくデータベーススキーマのためです。 Id_employeer列は自動インクリメントではありません。

+0

ありがとう、私はこれを2分前に解決しました。 私はその日の半分以上を無駄にしていました。 – Kimoki

関連する問題