Hibernate:How Override an Attribute from Mapped Super Class

Hibernate : How override an attribute from mapped super class

Try this, instead

@MappedSuperclass
public abstract class GenericEntity {
protected Integer id;
...

public Integer getId() {return id;}
public void setId(Integer id) {this.id = id;}
}

@Entity
@Table(name = "POJO_ONE")
@SequenceGenerator(name = "HB_SEQ_POJO_ONE", sequenceName = "SEQ_POJO_ONE", allocationSize = 1)
@AttributeOverride(name = "id", column = @Column(name = "ID"))
public class PojoOne extends GenericEntity {
// we should not define id here again
...

@Override
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "HB_SEQ_POJO_ONE")
public Integer getId() {return id;}
}

AttributeOverride MappedSuperClass attribute with a different type

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

How to override attribute from embedded column is superclass

Look, read documentation carefully:

To override mappings at multiple levels of embedding, a dot (".")
notation form must be used in the name element to indicate an
attribute within an embedded attribute.

The name "b" is incorrect.
You should use "key.b"

@Entity
@Table(name = "my_entity")
@AttributeOverride(name = "key.b", column = @Column(name="renamed_b"))
public class MyEntity extends Superclass
}

Hibernate and @AttributeOverride

Your problem is very easy to understand, if you know what the default inheritance type is: SINGLE_TABLE.

That means all entities that extending Person are in the same table. And thus Person already defines the ID column. Because you would otherwise violate the contract of the primary key column of your Person table.

I cite the JavaDoc of @AttributeOverride as well:

May be applied to an entity that extends a mapped superclass or to an embedded field or property to override a basic mapping or id mapping defined by the mapped superclass or embeddable class (or embeddable class of one of its attributes).

It always helps to read the JavaDoc first, before asking questions here.

What can you do about it? Make your Person a @MappedSuperclass (or create a BasePerson that is one).



Related Topics



Leave a reply



Submit