Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wrong TypeDescriptor when using JoinColumns in a composite Key

I've been banging my head around, trying to figure what's wrong with the following mapping. I understand the following mapping is not ideal for ORM, but that's how the database is and I cannot change its structure. I'm using JPA 2.1 and Hibernate 5.0.2.Final.

@MappedSuperclass    
public abstract class BaseEntity<T extends Serializable> implements Serializable {

    protected T id;

    @Id
    public T getId() {
        return id;
    }

    protected void setId(T id) {
        this.id = id;
    }
}

@Table(name = "campaign")
@AttributeOverride(name = "id", column = @Column(name = "campaign_id") )
public class Campaign extends BaseEntity<String> {
    // attributes, getters and setters
}

@Embeddable
public class CampaignBroadcastPK implements Serializable {

    @ManyToOne
    @JoinColumn(name = "campaign_id", insertable = false, updatable = false)
    private Campaign campaign;

    @Column(name = "broadcast_date")
    private LocalDate broadcastDate;

    // getters and setters
}

@Entity
@Table(name = "campaign_broadcast")
public class CampaignBroadcast implements Serializable {

    @EmbeddedId
    private CampaignBroadcastPK id;

    // attributes, getters and setters
}

@Embeddable
public class CampaignBroadcastProcessPK implements Serializable {

   @ManyToOne
   @JoinColumns({ 
       @JoinColumn(name = "campaign_id", insertable = false, updatable = false),
       @JoinColumn(name = "broadcast_date", insertable = false, updatable = false) 
    })
    private CampaignBroadcast broadcast;

    @Column(name = "process_date)
    private LocalDate processDate;

    // getters and setters
}

@Entity
@Table(name = "campaign_broadcast_process")
public class CampaignBroadcastProcess implements Serializable {

    @EmbeddedId
    private CampaignBroadcastProcessPK id;

    // attributes, getters and setters
}

Besides this structure, I also have a converter to handle LocalDate to java.sql.Date, which is annotated with autoApply=true.

When I try to load CampaignBroadcastProcess through entityManager.find(), hibernate tries to convert campaign_id to Date, even though it's mapped as a String, causing java.sql.Date to throw an IllegalStateException, because the String passed to valueOf isn't a valid date String.

I'm suspicious Hibernate is mixing up the types of the JoinColumns on CampaignBroadcastProcessPK, handling them both as a LocalDate.

Has someone ever faced this issue?

like image 490
renke Avatar asked Oct 19 '15 19:10

renke


1 Answers

I'm not exactly sure why, but changing the order of the two @JoinColum solved the issue. The mapping now is as below:

@ManyToOne
@JoinColumns({ 
    @JoinColumn(name = "broadcast_date", insertable = false, updatable = false),
    @JoinColumn(name = "campaign_id", insertable = false, updatable = false)     
})
private CampaignBroadcast broadcast;
like image 121
renke Avatar answered Nov 15 '22 14:11

renke