在下面的设置中,如何使继承的producerId
字段从Book
中变为updatable = false, insertable = false
?
@MappedSuperclass
public class StockItem {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column(name = "producer_id")
private Integer producerId;
}
@Entity
public class Producer {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column(name = "name")
private Integer name;
}
@Entity
@Table(name = "stock_item")
public class Book extends StockItem {
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "producer_id")
private Producer producer;
// make inherited producerId field updatable = false, insertable = false here
}
我特别不想执行以下操作,因为我需要该属性保持可更新和可插入,以供其他继承者使用:
@MappedSuperclass
public class StockItem {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column(name = "producer_id", updatable = false, insertable = false)
private Integer producerId;
}
UPDATE:另一个约束是扩展@MappedSuperclass的所有类都表示同一个表
2条答案
按热度按时间yrefmtwq1#
我发现的一个变通方法是在
producer
setter中设置producerId
,但这不是一个理想的解决方案。a7qyws3x2#
根据Asoub的建议,我使用
@AttributeOverride
执行了以下操作,它工作得很好,因为它允许修改所有列属性: