2

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

toscanelli
  • 1,202
  • 2
  • 17
  • 40
  • 1
    What was the SQL it used prior to the upgrade? Shouldn't the discriminator mapping be read-only, as it is supposed to be controlled by Java inheritance? Try marking it as insertable=false, updatable=false – Chris Sep 17 '15 at 13:25
  • Please post the stacktrace (at least the relevant part). And specify the Hibernate version. I would guess that it has to do with using the DiscrimatorColumn as field - I don't think that this is supported explicitly anywhere (especially for a modifiable field). If you really need to read the discriminator value, you could use a `@Formula` instead - or you have a method `getDiscriminator()` that you implement for every subclass of Observer. And you don't need to define the `InheritanceType` twice - only use it for the root entity. – Tobias Liefke Sep 17 '15 at 13:25
  • Both of you are right! Problem was on discriminator mapping. I added insertable=false, updatable=false and it works! I would give you the "best answer" check if I could. – toscanelli Sep 17 '15 at 13:58
  • insertable=false is ridiculous (though correct). Since the value is NOT defined on DB side, in MUST come as part of INSERT. Hence insertable=false does not make sense :) Look in the manual for @Column for this property: "Whether the column is included in SQL INSERT statements generated by the persistence provider." – Maksim Gumerov Jan 30 '18 at 08:52

2 Answers2

2

@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; }

Olcay Tarazan
  • 931
  • 9
  • 12
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.

DᴀʀᴛʜVᴀᴅᴇʀ
  • 7,681
  • 17
  • 73
  • 127
dassum
  • 4,727
  • 2
  • 25
  • 38