I have the following simple example. When it runs, I see the QuoteRequest object being generated with auto-generated id. Next, I see the Accidents object being generated, but quote_request_id that's being inserted is null, so I get an error:
Column 'quote_request_id' cannot be null
@Entity
@Table(name = "Quotes")
public class QuoteRequest
{
public QuoteRequest(){}
@Id
@Column(name = "quote_request_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long QuoteRequestId;
@OneToMany(mappedBy = "quoteRequest", cascade = CascadeType.ALL)
@OrderColumn(name = "accidents_id")
private Accidents[] accidents;
// Getters and setters
}
@Entity
@Table(name = "Accidents")
public class Accidents
{
public Accidents()
{
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "accidents_id")
private long AccidentId;
@Column(name = "amount", nullable = false)
private Float amount;
@ManyToOne(optional = false, cascade = CascadeType.ALL)
@JoinColumn(name = "quote_request_id", nullable = false)
private QuoteRequest quoteRequest;
// Getters and setters
}
Why isn't it inserting the newly generated ID into the accidents column?
Accidents
get inserted, it should havequote_request_id
insert together. May I know which version of Hibernate you are using? And, more important: for theAccidents
object you created, have you actually setup itsquoteRequest
correctly? (I wish you are only setting theaccidents
onQuoteRequest
side, but forgot thequoteRequest
on theAccidents
side, as the latter is the side that owns the relationship). One OT issue: You should name itAccident
instead ofAccidents
, as it represent one accident only