IQbrod
IQbrod

Reputation: 2285

AttributeOverride MappedSuperClass attribute with a different type

I have an Abstract class used as MappedSuperclass like :

@MappedSuperclass
@IdClass(LogId.class)
public abstract class Log {
    private LocalDateTime moment;
    private String pid;
    // attributes omitted
    private String type;
    private String message;

    @Id
    public LocalDateTime getMoment() { return moment; }
    @Id
    public String getPid() { return pid; }
    // getters/setters omitted
}

My Log class provides to every entity the same structure with standard attributes type and message. Each entity represents a table in my database which share every attributes (@Id and // omitted ones). Columns type and message might have different names & types.
My first attempt was :

@Entity
@Table(name = "flow_catcher")
public class FlowCatcher extends Log {
    @Override
    @Column(name = "message_type")
    public String getType() {
        return super.getType();
    }

    @Override
    @Column(name = "count")
    @Convert(converter = StringToIntegerDbConverter.class)
    public String getMessage() {
        return super.getMessage();
    }

}

Following this post I used @AttributeOverride like :

@Entity
@Table(name = "flow_catcher")
@AttributeOverride(name = "type", column = @Column(name = "message_type"))
@AttributeOverride(name = "message", column = @Column(name = "count"))
public class FlowCatcher extends Log {

}

But it seems impossible to use any converter to get data count as a String and not an Integer.
Here is my error log:

Unable to build Hibernate SessionFactory; nested exception is
org.hibernate.tool.schema.spi.SchemaManagementException:
    Schema-validation: wrong column type encountered in column [count] in table [flow_catcher];
    found [int (Types#INTEGER)], but expecting [varchar(255) (Types#VARCHAR)]

Is there any way to get my column count as a String and not an Integer ?

PS: Hibernate hbm2ddl.auto is set on validate and database scheme can't change.


EDIT 1: I found a working solution but it uses another (unused) column in my database other_string_column which doesn't sound clean to me :

@Entity
@Table(name = "flow_catcher")
@AttributeOverride(name = "type", column = @Column(name = "message_type"))
@AttributeOverride(name = "message", column = @Column(name = "other_string_column"))
public class FlowCatcher extends Log{

    private Integer count;

    @Override
    public String getMessage() {
        return this.count.toString();
    }

    public Integer getCount() { return count; }
    public void setCount(Integer count) { this.count = count; }
}

Upvotes: 0

Views: 837

Answers (1)

Bhushan Uniyal
Bhushan Uniyal

Reputation: 5703

You can go with generic

@MappedSuperclass
@IdClass(LogId.class)
public class Log<T extends Comparable> implements Serializable {
     @Id
     private LocalDateTime moment;
     @Id
    private String pid;
    //additional required fields here
    
    // Do NOT  SPECIFY MAPPING
    private T message;

}


@Entity
@Table(name = "flow_catcher")
@AttributeOverride(name = "type", column = @Column(name = "message_type"))
@AttributeOverride(name = "message", column = @Column(name = "count", columnDefinition = "BIGINT(15)"))
public class FlowCatcher extends Log<Integer> {
    private static final long serialVersionUID = -3629698185247120860L;
}



@Entity
@Table(name = "flow_catcher_another_sample")
@AttributeOverride(name = "type", column = @Column(name = "message_type"))
@AttributeOverride(name = "message", column = @Column(name = "message", columnDefinition = "VARCHAR(20)"))
public class FlowCatcherString extends Log<String> {
    private static final long serialVersionUID = -3629698185247120860L;
}

Upvotes: 3

Related Questions