Reputation: 393
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;
import lombok.NoArgsConstructor;
@Data
@Entity
@NoArgsConstructor
@AllArgsConstructor(onConstructor = @__({@JsonIgnoreProperties(ignoreUnknown = true)}))
@Table(name = "ATTRIBUTE_SCORE")
@JsonInclude(JsonInclude.Include.NON_NULL)
public class AttributeScore implements Serializable {
/**
*
*/
private static final long serialVersionUID = 621935018037696921L;
public final static int ABOVE_VALUE = 80;
public final static int AT_VALUE = 70;
@Getter(onMethod = @__({
@Id,
@GeneratedValue(strategy = GenerationType.IDENTITY),
@Column(name = "ID")
}))
private Long id;
/** Attribute Score */
@Getter(onMethod = @__({
@ManyToOne(fetch = FetchType.EAGER),
@JoinColumn(name = "ATTRIBUTE_ID")
}))
private Attribute attribute;
/** Score */
@Getter(onMethod = @__({
@Column(name = "SCORE")}))
private int score;
}
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;
import lombok.NoArgsConstructor;
@Data
@Entity
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "ATTRIBUTES")
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Attribute implements Serializable {
/**
*
*/
private static final long serialVersionUID = -8847589625501522335L;
public static final int MAX_POINTS = 3;
/** Attribute id */
@Getter(onMethod = @__({
@Id,
@GeneratedValue(strategy = GenerationType.IDENTITY),
@Column(name = "ID")
}))
private Long id;
/** Attribute Name */
@Getter(onMethod = @__({
@Column(name = "NAME", length = 500)}))
private String name;
/** Weight */
@Getter(onMethod = @__({
@Column(name = "WEIGHT")}))
private int weight;
/** Points */
@Getter(onMethod = @__({
@Column(name = "DISPLAY_ORDER")}))
private int displayOrder;
/** Required */
@Getter(onMethod = @__({
@Column(name = "REQUIRED")}))
private boolean required;
}
@Stateless
@LocalBean
class. protected E saveInstance(E entity) {
if (entity == null) {
throw new NosisEJBValidationException("entity cannot be null");
} else {
I id = getId(entity);
if (id == null) {
em.persist(entity);
logger.info(Logger.EVENT_UNSPECIFIED, "entity of type '" + entity.getClass().getSimpleName()
+ "' with id '" + getId(entity) + "' created.");
} else {
entity = em.merge(entity);
logger.info(Logger.EVENT_UNSPECIFIED,
"entity of type '" + entity.getClass().getSimpleName() + "' with id '" + id + "' updated.");
}
}
return entity;
}
calling the above saveInstance
method results in the following:
SQL Error: 1400, SQLState: 23000
ORA-01400: cannot insert NULL into ("COMMON_USER"."ATTRIBUTE_SCORE"."ID")
The question's name pretty much says it all. I am trying to persist a new instance of ATTRIBUTE_SCORE
. The entity
being passed into saveInstance
has an id
of null
. Any idea why this isn't working.
I honestly do not know where to look.
EDIT
it turns out that the wrong sql is being generated:
Hibernate: insert into common_user.ATTRIBUTE_SCORE (ATTRIBUTE_ID, SCORE) values (?, ?)
it should be:
Hibernate: insert into common_user.ATTRIBUTE_SCORE (ID, ATTRIBUTE_ID, SCORE) values (?, ?, ?)
so new question: why is the id field being excluded?
Upvotes: 1
Views: 8673
Reputation: 2832
If you use the strategy javax.persistence.GenerationType.IDENTITY for @GeneratedValue your table must have an identity generator. This can be done including an AUTO_INCREMENT to your primary key.
if you don't set any value for your id field, it is supposed to be null or default value (or it will do an auto increment if you set that attribute).
The @GeneratedValue annotation just tells Hibernate that the database is generating this value itself. So the AUTO_INCREMENT should be defined in the database as well.
Similar question at this link, Hibernate @GeneratedValue null error for primary key
Upvotes: 3