Paulo Galdo Sandoval
Paulo Galdo Sandoval

Reputation: 2203

Mapping three identical tables in one entity

i'm needing your help.

i have 3 tables on my database running on postgresql. they are exactly the same structure. so i thought if i could mapp them in one entity.

i try this:

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

but when i try to look in to one table i only get data from the first 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;

some idea what i'm doing wrong?

Upvotes: 1

Views: 82

Answers (1)

mtj
mtj

Reputation: 3554

@SecondaryTables is used to denote one entity being spread over multiple tables in terms of columns, not as a union.

The only thing I can think of right now is using a view which does a union over all the tables, but I am not sure whether postgres can handle writable views, or how you declare them (if you even need a write interface.)

Upvotes: 1

Related Questions