我需要你的帮助。
我的数据库中有 3 个表在 postgresql 上运行。它们的结构完全相同。所以我想是否可以将它们映射到一个实体中。
我试试这个:
@Entity
@Table(name = "stock_tierra")
@SecondaryTables({
@SecondaryTable(name = "stock_bebelandia"),
@SecondaryTable(name = "stock_libertador")
})
public class Stock implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id_stock", unique = true, nullable = false)
private int idStock;
@Column(name = "cantidad", nullable = false)
private int cantidad;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "id_producto", nullable = false)
private Producto idProducto;
@Column(name = "estado", nullable = false)
private boolean estado;
@Column(name = "usuario_creacion", nullable = false)
private int usuarioCreacion;
@Column(name = "usuario_modificacion")
private Integer usuarioModificacion;
@Temporal(TemporalType.DATE)
@Column(name = "fecha_creacion", nullable = false, length = 13)
private Date fechaCreacion;
@Temporal(TemporalType.DATE)
@Column(name = "fecha_modificacion", length = 13)
private Date fechaModificacion;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "id_sucursal", nullable = false)
private Sucursal idSucursal;
但是当我尝试查看一个表时,我只从第一个 stock_tierra
String stockSucursal = null;
switch (sucursal) {
case 1:
stockSucursal = "stock_tierra";
break;
case 2:
stockSucursal = "stock_bebelandia";
break;
case 3:
stockSucursal = "stock_libertador";
break;
}
Criteria criteria = getSession().createCriteria(Stock.class, stockSucursal);
criteria.add(Restrictions.eq("estado", true));
criteria.addOrder(Order.asc("idStock"));
List<Stock> list = criteria.list();
return list;
知道我做错了什么吗?
最佳答案
@SecondaryTables 用于表示一个实体以列的形式分布在多个表中,而不是作为一个联合。
我现在唯一能想到的是使用一个对所有表进行联合的 View ,但我不确定postgres是否可以处理可写 View ,或者如何声明它们(如果你甚至需要一个写接口(interface)) .)
关于java - 在一个实体中映射三个相同的表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36949923/