更新
我的朋友做了相同的代码行,他的行也正常工作。会是什么呢?我使用NetBeans 7.0.1,他的是6.9.1。我的GlassFish是3.1,他是3.0.1
END OF UPDATE
@Inheritance(strategy= InheritanceType.JOINED)
@Entity
@Table(name = "vehicle")
@DiscriminatorColumn(name = "type", discriminatorType = DiscriminatorType.INTEGER)
@NamedQueries({
@NamedQuery(name = "Vehicle.findAll", query = "SELECT v FROM Vehicle v")})
public class Vehicle implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Integer id;
@JoinColumn(name = "color_id")
@ManyToOne(optional = false)
private Color color;
public Vehicle() {
}
//Getters and Setters
}
---
@Entity
@Table(name = "car")
@DiscriminatorValue("1")
@NamedQueries({
@NamedQuery(name = "Car.findAll", query = "SELECT c FROM Car c")})
public class Car extends Vehicle implements Serializable {
private static final long serialVersionUID = 1L;
@Column(name = "airbag")
private Boolean airbag;
public Car() {
}
//Getters and Setters
}
当我试图删除一辆汽车时,它会抛出一个异常,并且在Stacktrace中有一个UPDATE查询,如果我想删除它,这很奇怪。
你们有没有想过这里会发生什么?
Car car = em.find(Car.class, id);
em.remove(car);
堆栈:
Caused by: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.0.v20110604-r9504): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'color_id' cannot be null
Error Code: 1048
Call: UPDATE vehicle SET color_id = ? WHERE (id = ?)
bind => [2 parameters bound]
Query: DeleteObjectQuery(br.com.something.entity.Vehicle[ id=3 ])
at org.eclipse.persistence.exceptions.DatabaseException.sqlException(DatabaseException.java:324)
at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.executeDirectNoSelect(DatabaseAccessor.java:840)
at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.executeNoSelect(DatabaseAccessor.java:906)
at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.basicExecuteCall(DatabaseAccessor.java:592)
at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.executeCall(DatabaseAccessor.java:535)
at org.eclipse.persistence.internal.sessions.AbstractSession.basicExecuteCall(AbstractSession.java:1702)
at org.eclipse.persistence.sessions.server.ClientSession.executeCall(ClientSession.java:253)
at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.executeCall(DatasourceCallQueryMechanism.java:207)
at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.executeCall(DatasourceCallQueryMechanism.java:193)
at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.updateObject(DatasourceCallQueryMechanism.java:749)
答案 0 :(得分:1)
也许你需要一个关于汽车和颜色之间关系的CascadeType?