gpt4 book ai didi

java - 使用 JPA 删除实体时出现问题

转载 作者:太空宇宙 更新时间:2023-11-04 08:32:14 25 4
gpt4 key购买 nike

更新

我的 friend 做了同样的代码,他的代码行正常工作。会是什么呢?我用的是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?

关于java - 使用 JPA 删除实体时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7271511/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com