简体   繁体   中英

Mapping multiple Java objects to different rows in same table with Hibernate

I've looked around for this but most answers related to multiple objects per table are talking about inheritance or something just a bit different than what I am looking for.

I have a couple tables, lets call them Vehicle and Order :

╔════╦════════════╗    ╔════╦════════════╗
║    ║  Vehicle   ║    ║    ║   Order    ║
╠════╬════════════╣    ╠════╬════════════╣
║ PK ║ id         ║    ║ PK ║ id         ║
║    ║    ...     ║    ║    ║    ...     ║
║    ║ address_id ║    ║    ║ address_id ║
║    ║ address    ║    ║    ║ address    ║
║    ║ city       ║    ║    ║ city       ║
║    ║ state      ║    ║    ║ state      ║
║    ║ zip        ║    ║    ║ zip        ║
╚════╩════════════╝    ╚════╩════════════╝

As you can see, the address information is in each table. There are other fields as denoted by ... that are specifically for each table.

What I want to do is map the address information in these tables to an Address object in Java to be handled separately. I've started with something like this for each of these tables:

@Data
@Entity
@Table(name = "vehicle")
@SuppressWarnings("serial")
public class Vehicle implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "vehicle_id")
    private int id;

    // other fields

    @Valid
    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "address_id")
    private Address address;

}

@Data
@Entity
@Table(name = "order")
@SuppressWarnings("serial")
public class Order implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "order_id")
    private int id;

    // other fields

    @Valid
    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "address_id")
    private Address address;

}

These objects are not related besides being in the same application, so no inheritance or anything here. I would like to have a separate Address object that both of these objects have, and the address information for the table is pulled from and pushed to that.

My question is, is this possible the way I am thinking of it? This is the way the database will be set up, so that's why I'm looking for a workaround; I understand it would probably be better to have a separate table for the addresses. I could potentially just save the address information in each object separately without an Address object, but I figured having a separate Address object would help with certain operations.

It seems what you're looking for are JPA's @Embeddable s, see eg https://en.wikibooks.org/wiki/Java_Persistence/Embeddables

You'd annotate Address as @Embeddable and its fields will magically be duplicated into each entity that holds an Address .

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