简体   繁体   中英

Mapping multiple columns from a legacy database table to a (java) List in hibernate?

When dealing with a denormalised (legacy) database table, whereby you have columns such as:

price_1, price_2, price_3, ..., price_10

What is the most efficient and cleanest way of mapping such columns into a java collection (a List for example), instead of mapping them to corresponding price1, price2, ... properties.

Is there a simple way of doing it that does not involve UserTypes.

I am talking about columns residing on the same table, not the usual one-to-many relationship between multiple entities.

What I've done once before in this situation is create a class to encapsulate the prices, like

public class Prices {
    Integer price_1;
    Integer price_2;

    public List<Integer> getAllPrices() {
        List<Integer> allPrices = new ArrayList<Integer>();
        allPricess.add(price_1);
        allPrices.add(price_2);
        return allPrices;
    }
    public Integer getPrice(int priceNumber) {
        if (priceNumber == 1) {
            return price_1;
        }
        // etc...
    }
}

Then just map the columns in the table to the individual fields and use the Java class to hide the actual structure from the rest of the code. You can access all the prices as a collection or individually or whatever is suitable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM