Error generating Id automatically in Hibernate

Asked

Viewed 156 times

1

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

"ids for this class must be Manually Assigned before Calling save()"

The class that’s saying you’re 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 switching 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?

  • Try adding the following annotations: @Basic(optional = false)
 @Column(name = "id_todo",unique=true, nullable = false)

  • I had already seen this solution in another topic. It didn’t work either.

1 answer

2


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

It used to be 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 tag

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.