Error generating Id automatically in hibernate

1

I am making an application with hibernate, but it is generating and error:

"ids for this class must be manually assigned before calling save ()"

The class that is saying it is wrong is like this.

@Entity
@Table(name = "USER")
public class TodoModel {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Long id_todo;
    private String nomeTodo;
    private String usuario;
...Getters/Setters/HashCode/Equals..

And the table was created this way

CREATE TABLE USER (
id_todo INT(5) NOT NULL AUTO_INCREMENT,
nomeTodo VARCHAR(15),
usuario VARCHAR(15),
PRIMARY KEY (id_todo)
);

Even changing the GenerationType method to IDENTITY did not work. The solutions I see around always suggest putting IDENTITY or AUTO that would work, but here it did not work.

What can it be?

    
asked by anonymous 01.09.2018 / 19:09

1 answer

2

Reading the documentation I saw that a tag was missing in the xml that maps the class

Before it was like this:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="aprendizagem.model.TodoModel" table="USER">
        <id name="id_todo" type="java.lang.Long">
            <column name="id_todo" />
        </id>
        <property name="nomeTodo" type="java.lang.String">
            <column name="nomeTodo" />
        </property>
        <property name="usuario" type="java.lang.String">
            <column name="usuario" />
        </property>

    </class>
</hibernate-mapping>

and now adding the

<generator class="increment" />

Code

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="aprendizagem.model.TodoModel" table="USER">
        <id name="id_todo" type="java.lang.Long">
            <column name="id_todo" />
            <generator class="increment" />
        </id>
        <property name="nomeTodo" type="java.lang.String">
            <column name="nomeTodo" />
        </property>
        <property name="usuario" type="java.lang.String">
            <column name="usuario" />
        </property>

    </class>
</hibernate-mapping>
    
02.09.2018 / 06:58