Problem removing entity with JPA

2019-05-31 13:09发布

UPDATE

My friend did the same lines of code and his lines just worked. What could it be? I use NetBeans 7.0.1 and his is 6.9.1. My GlassFish is 3.1 and his is 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

}

When I'm trying to remove a car it thorws an Exception and in the Stacktrace has a UPDATE query which is pretty weird if I'm trying to remove.

Do you guys have any idea of what might be going on here?

Car car = em.find(Car.class, id);
em.remove(car);

Stack:

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)

1条回答
叼着烟拽天下
2楼-- · 2019-05-31 14:03

Perhaps you need a CascadeType on the relation between car and color?

查看更多
登录 后发表回答