更新

我的朋友做了相同的代码行,而他的代码行得通。会是什么呢?我使用NetBeans 7.0.1,而他是6.9.1。我的GlassFish是3.1,而他是3.0.1

更新结束

@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)

最佳答案

也许您需要有关汽车和颜色之间关系的CascadeType?

09-28 02:25