问题
I have two entities for two classes. First is an extended class of the second (Observer pattern): The child:
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorValue("User")
@Table(name="SCH.USER")
public class User extends Observer implements Serializable{
...fields...
}
And the father:
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name = "DISCRIMINATOR", discriminatorType = DiscriminatorType.STRING)
@Table(name="SCH.OBSERVER")
public abstract class Observer implements Serializable{
@Id
@SequenceGenerator(name = "OBSERVER_ID_GENERATOR", sequenceName = "NEXO.SEQ_OBSERVER", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "OBSERVER_ID_GENERATOR")
@Column(name="ID_OBSERVER")
private Long idObserver;
@Column(name = "DISCRIMINATOR", nullable=false, length=20)
private String discriminator;
}
Since I upgraded JPA 2.0 to JPA 2.1 I cannot persist the child entity: em.persist(userInstance); Which generates the next SQL:
insert
into
NEXO.OBSERVER
(DISCRIMINATOR, ID_OBSERVER)
values
('User', ?)
And gets the resulting error:
java.sql.SQLException: invalid column index
I am pretty sure this behavior change has been produced by the upgrade to JPA 2.1
回答1:
@Chris is right. When I upgrade from Jboss7 to Wildfly10, noticed this error and fixed by updating discriminator
@Column(name = "INPUT_TYPE", nullable = false, length = 6, insertable = false, updatable = false) public String getInputType() { return inputType; }
回答2:
I faced the same problem:
ERROR SqlExceptionHelper:146 - The column index is out of range: 2, number of columns: 1
When I upgraded the JPA version to add the following two attributes to the discriminator column:
insertable = false
and updatable = false
Earlier this was the default.
来源:https://stackoverflow.com/questions/32630548/error-when-trying-to-insert-an-extended-entity-invalid-column-index