0
votes

I don't understand why my app drop Exception "Field 'Id_employeer' doesn't have a default value".

Id_employeer is my primary key, when I try to save any information to my database I got that exception, but, when I try just to read info from DB it's working.

I read topic on StackOverflow, but I still don't understand the answers in my app. I'm working with Hibernate 5.2.11.

My 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>

My 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);
        }
    }




}

Next CharacterInfo class

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;
    }



}

main class

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);


    }
}

and in the end Exception

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

1 Answers

1
votes

First of all, if you're using Hibernate annotations, you don't need to use Hibernate XML mapping files.

Your error is probably because of your database schema. The Id_employeer column is not auto-increment.