Caused by: org.hibernate.MappingException: subclass key mapping has wrong number of columns Play framework


I was trying to implement the Joined Table inheritance strategy of JPA

The Joined Table inheritance strategy shares a referenced column that contains unique elements that are common to child tables.


This is the parent table

@Entity
@Table
@Inheritance( strategy = InheritanceType.JOINED )
public class UserAlertConfiguration extends Model implements Serializable {

    @Id
    @GeneratedValue( strategy = GenerationType.AUTO )
    private Long id;
    
    @Column(name = "alert_name", nullable = false)
    private String alertName;
    
    @Column(name = "communication_method", nullable = false)
    @Enumerated(EnumType.STRING)
    private CommunicationMethod method;
    
    @Column(name = "to_address", nullable = false)
    private String toAddress;

    @Column(name = "user_profile_id", nullable = false)
    private long userProfileId;
    
    @Column(name = "added_at", insertable = true, updatable = false)
    @Temporal(TemporalType.TIMESTAMP)
    private Date addedAt = new Date();

    @Column(name = "updated_at", insertable = true, updatable = true)
    @Temporal(TemporalType.TIMESTAMP)
    public Date updatedAt = new Date();
    
    @Column(name = "last_processed_alert_timestamp", nullable = true)
    private Long lastProcessedAlertTimeStamp;

The following are the chilren
Child 1
@Entity
@PrimaryKeyJoinColumn(referencedColumnName="id")
public class ConnectionTimeAlert extends UserAlertConfiguration
{
    
    @Column(name = "rig_uid", nullable = false)
    private String rigUid;
    
    @Column(name = "rig_name", nullable = false)
    private String rigName;
    
    @Column(name = "surface_target", nullable = true)
    private Float surfaceTarget;
    
    @Column(name = "intermediate_target", nullable = true)
    private Float intermediateTarget;
 
    @Column(name = "curve_target", nullable = true)
    private Float curveTarget;
    
    @Column(name = "lateral_target", nullable = true)
    private Float lateralTarget;

Child 2
@Entity
@PrimaryKeyJoinColumn(referencedColumnName="id")
public class ROPAlert extends UserAlertConfiguration {

    @Column(name = "rig_uid", nullable = false)
    private String rigUid;
    
    @Column(name = "rig_name", nullable = false)
    private String rigName;
    
    @Column(name = "active_well_uid", nullable = true)
    private String activeWellUid;
    
    @Column(name = "offset_well_uid", nullable = true)
    private String offsetWellUid;
    
    @Column(name = "avgROP_deviation_percent", nullable = true)
    private Float avgROPDeviationPercent;
 
    @Column(name = "moving_footage", nullable = true)
    private Float movingFootage;



When i tried to run the application in Play framework. Caused by: org.hibernate.MappingException: subclass key mapping has wrong number of columns: models.com.moblize.model.ROPAlert type: component[id,id]

The root cause is that, when we extend the entity classes with Model, (This is the main helper class for JPA in Play Framework, the play.db.jpa.Model class automatically provides an autogenerated Long id field. ) Model implicitly adds an id field. By adding my own id field may cause problems. When i removed the id column from UserAlertConfiguration class the issue is resolved.
More Info on Play JPA https://www.playframework.com/documentation/1.2.1/jpa#support


You can use GenericModel if you want some control on your id and sequence strategy
Custom id mapping with GenericModel:
Nothing forces you to base your entities on play.db.jpa.Model. Your JPA entities can also extend the play.db.jpa.GenericModel class. This is required if you do not want to use a Long id as the primary key for your entity.


Hope this helps:))

No comments:

 Python Basics How to check the version of Python interpreter mac terminal

Popular in last 30 days