I get a ConstrainViolationException. It's in german, I hope the translation is accurate:
Quote:
Update or delete on table "color" violates foreign key constraint "1234" on table "vehicle". The key is still referenced from table "vehicle"
Unfortunately the problem is still present. The only thing I can do to get around it is to alter the foreig key constraint and add the option "ON DELETE SET NULL" which is "ON DELETE NO ACTION" by default.
Here is my code:
Vehicle.java
Code:
@Entity
@SequenceGenerator(name="vehicle_seq",sequenceName="vehicle_id_seq")
public class Vehicle{
private int id;
private String name;
private Color color;
@Id @GeneratedValue(generator="vehicle_seq",strategy=GenerationType.SEQUENCE)
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@ManyToOne
@JoinColumn(nullable=true)
public Color getColor() {
return color;
}
public void setColor(Color color) {
this.color = color;
}
}
Color.java:
Code:
@Entity
@SequenceGenerator(name="color_seq",sequenceName="color_id_seq")
public class Color {
public int id;
public String name;
@Id @GeneratedValue(generator="color_seq",strategy=GenerationType.SEQUENCE)
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
I'm using a PostgreSQL database. The tables generated by hbm2ddl look as follows:
Vehicle table:
Code:
CREATE TABLE vehicle
(
id integer NOT NULL,
name character varying(255),
color_id integer,
CONSTRAINT vehicle_pkey PRIMARY KEY (id),
CONSTRAINT fk27fbe3feda3c3806 FOREIGN KEY (color_id)
REFERENCES color (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION
)
WITHOUT OIDS;
ALTER TABLE vehicle OWNER TO dankof;
Color table:
Code:
CREATE TABLE color
(
id integer NOT NULL,
name character varying(255),
CONSTRAINT color_pkey PRIMARY KEY (id)
)
WITHOUT OIDS;
ALTER TABLE color OWNER TO dankof;