1

I've got an entity with a @GenericGenerator:

@Entity
public class Report {
    @Id
    @GenericGenerator(name = "generator", strategy = "foreign",
            parameters = @Parameter(name = "property", value = "examrequisitions"))
    // @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(nullable = false, unique = true)
    private Long id;

    ... // outcome, status
}

When I save that entity it shows the following exception:

Caused by: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'id' cannot be null
Error Code: 1048
Call: INSERT INTO SystemCardiologyReports.REPORTS (ID, OUTCOME, STATUS) VALUES (?, ?, ?)
    bind => [3 parameters bound]
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'id' cannot be null

Why is the ID null - despite the @GenericGenerator?

Tobias Liefke
  • 8,637
  • 2
  • 41
  • 58
developer033
  • 24,267
  • 8
  • 82
  • 108
  • I think that the error message is clear: it is missing the id for your entity. You did not add `@GeneratedValue(generator = "generator")` to your ID. The `@GenericGenerator` alone does not indicate to generate a value for the ID. – Tobias Liefke Nov 17 '15 at 09:17
  • I think you didn't read well, the initial lines of "Model Reports" contains it. – developer033 Nov 17 '15 at 14:08
  • You mean `// @GeneratedValue(strategy = GenerationType.IDENTITY)`? You should check the definition of `//` (= line comments). And even without the comment, you are not referencing the generator. – Tobias Liefke Nov 17 '15 at 14:20
  • Dude, I don't want to be rude, but of course I know what // means, now I'll ask for you: did you see that the line is commented is the of IDENTITY and the other not? Of course, I'm trying to generate an id based on the selectOneMenu, as I asked in question. `@GenericGenerator(name = "generator", strategy = "foreign", parameters = @Parameter(name = "property", value = "examrequisitions"))` for me it's not commented. =) – developer033 Nov 17 '15 at 14:27

1 Answers1

2

A GenericGenerator annotation just declares a generator somewhere in your code. You can add it to any package, type, field or method. It does not imply that the generator should be used for the annotated element.

You need to add @GeneratedValue to the field where you want to use it and reference it by its name:

@Id
@GenericGenerator(name = "genericGenerator" ...))
@GeneratedValue(generator = "genericGenerator")
private Long id;
Tobias Liefke
  • 8,637
  • 2
  • 41
  • 58
  • Man, really thanks for your help, it's working now, I thought I was posted with that GeneratedValue Annotation, it happened because I changed the code many times, so I got confused. Sorry and thanks so much. – developer033 Nov 17 '15 at 15:09
  • 1
    No problem. I stripped irrelevant information from your question to help persons with similar problems. – Tobias Liefke Nov 17 '15 at 15:39