简体   繁体   中英

JPA (hibernate) mapping child objects by field

EDIT: For those who may pass through here in the future, I'd like to share something I found. Although it's not going to work for this particular situation, hibernate does have an @Where annotation. With this annotation you could have N amount of Sets. Each set would have @Where(clause='column=value'). I will not be using the @Where solution but it may work out for you.

I'm new(ish) to JPA Hibernate and am looking for some help. I don't necessarily know what to search for either so here is a quick example of my problem.

I need to take a Set and split it into multiple Set dependent on a value of a field.

Lets look at something simple:

public class Customer{
    private String name;
    private String age;
    private Set<Order> orders;
}

public class Order{
    private int amount;
    private String status;
}

Goal: I would like to have Hibernate split my Customer's orders by their status into seperate Sets. Currently I use the @POSTLOAD annotation to loop over the Set and seperate them out accordinly. I would prefer Hibernate do it for me resulting in something like:

public class Customer{
    //Irrelevant stuff from above...

    private Set<Order> pendingOrders;
    private Set<Order> completedOrders;
    private Set<Order> canceledOrders;
}

These sets would be based off the order's status.

  • pendingOrders is where Order.getStatus() == "PENDING"
  • completedOrders is where Order.getStatus() == "COMPLETED"
  • canceledOrders is where Order.getStatus() == "CANCELED"

The tables are directly represented by the classes and are both considered "Entities". (Customer and Order)

If I haven't provided adequate information, could you please point me to a proper search for what I am "trying" to accomplish. In terms of JPA terminology I have no idea what to search for and could really use some help!

Thank you!

The best approach in your case is create a discriminator column and some extra classes to use it. Example

@Entity
@Table(name = "ORDER")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "STATUS", discriminatorType = DiscriminatorType.STRING)
public abstract class Order{
}


@Entity
@DiscriminatorValue(value = "PENDING")
public class PendingOrder extends Customer {

}

public class Customer{
    //Irrelevant stuff from above...

    private Set<PendingOrder> pendingOrders;
    //same fo0r all others types
}

And if you notice the orders have some specific information only valid for pending orders or completed orders, then probably the best approach is use

@Inheritance(strategy = InheritanceType.JOINED)

Or

@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)

And add the specific columns in those new tables

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