Hibernate:如何覆盖映射超类的属性
通用实体,超类:
@MappedSuperclass
public abstract class GenericEntity {
private Integer id;
public Integer getId() {return id;}
public void setId(Integer id) {this.id = id;}
}
pojo:
@Entity
@Table(name = "POJO_ONE")
@SequenceGenerator(name = "HB_SEQ_POJO_ONE", sequenceName = "SEQ_POJO_ONE", allocationSize = 1)
public class PojoOne extends GenericEntity {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "HB_SEQ_POJO_ONE")
@Column(name = "ID")
@AttributeOverride(name = "id", column = @Column(name = "ID"))
private Integer id;
@Override
public Integer getId() {return id;}
}
我尝试使用这些注释:@AttributeOverride,@Id,...但它不起作用。你能帮助我吗? 我想覆盖属性“id”以指定另一个列名和 pojo/table 的序列。 最好的方法是什么?
The generic entity, super class:
@MappedSuperclass
public abstract class GenericEntity {
private Integer id;
public Integer getId() {return id;}
public void setId(Integer id) {this.id = id;}
}
The pojo:
@Entity
@Table(name = "POJO_ONE")
@SequenceGenerator(name = "HB_SEQ_POJO_ONE", sequenceName = "SEQ_POJO_ONE", allocationSize = 1)
public class PojoOne extends GenericEntity {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "HB_SEQ_POJO_ONE")
@Column(name = "ID")
@AttributeOverride(name = "id", column = @Column(name = "ID"))
private Integer id;
@Override
public Integer getId() {return id;}
}
I try to use thoses annotations : @AttributeOverride, @Id, ... but It doesn't work. Can you help me?
I want to override the attribute "id" to specify another column name and a sequence by pojo/table.
What is the best way to do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个
Try this, instead
为什么不用
@Id
注释GenericEntity的id
呢?您也不应该重新定义
id
,而是将@AttributeOverride(name = "id", column = @Column(name = "ID"))
放在类上而不是场地。编辑:
我们在基类中使用它(
package.OurTableGenerator
是我们自己的实现):这让我们指定每个实体/表的不同块大小和序列。
对于您自己的表生成器,您可以对 org.hibernate.id.TableGenerator 进行子类化。
Why don't you annotate the
id
of GenericEntity with@Id
?You also should not redefine
id
but put the@AttributeOverride(name = "id", column = @Column(name = "ID"))
on the class rather than a field.Edit:
We're using this in our base class (
package.OurTableGenerator
is our own implementation):This let's us specify a differenc block size and sequence per entity/table.
For your own table generator you could subclass
org.hibernate.id.TableGenerator
.