Search code examples
javamysqljpaeclipselinkspring-data-jpa

Insert row to table with the only identifier column with EclipseLink


I have an entity:

@Entity
public class MyEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = Columns.ID)
    private Long id;

    // getter & setter
}

Then, I create an instance of the entity MyEntity my = new MyEntity(); and want to save it with DAO (using Spring Data for JPA) - dao.save(my);. I got the following exception:

Caused by: Exception [EclipseLink-6023] 
(Eclipse Persistence Services - 2.4.0.v20120608-r11652): 
org.eclipse.persistence.exceptions.QueryException
Exception Description: The list of fields to insert into the table
[DatabaseTable(my_entity)] is empty.  
You must define at least one mapping for this table.
Query: InsertObjectQuery(null)

When I add another, useless column to the entity with a default value

private int nothing = 6;

Exception disappears. Any solution for that?

This topic does not help me. I use EclipseLink + MySQL + Spring Data.


Solution

  • This is because you have no fields, and are using a generated id, so there is nothing to insert. You either need to add another field, or need to use TABLE id generation instead of IDENTITY (I recommend never using IDENTITY as it does not support pre-allocation).