Home > Software design >  How to map multiple classes to one table in JPA?
How to map multiple classes to one table in JPA?

Time:05-26

I know this is a frequently asked question, but I couldn't find any answers because I have 3 classes and I generally have problems to build the given structure:

type OrderItem = {
    count: number,
    price: number,
    order: number,
    subItems: {
        count: number,
        name: string,
        price: number,
        extraItems: {
            count: number,
            name: string,
            price: number,
        }
    }
};

This is my try at doing it in Java with JPA:

Order.java

package de.gabriel.mcdonaldsproject.models;

import javax.persistence.*;
import java.io.Serializable;
import java.util.List;


@Entity
@Table(name = "orders", schema = "public")
public class Order implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "order_generator")
    @SequenceGenerator(name = "order_generator", sequenceName = "order_seq")
    private long id;
    private List<Item> products; // <--------- 'Basic' attribute type should not be a container

    public Order() {
    }

    public Order(List<Item> products) {
        this.products = products;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public List<Item> getProducts() {
        return products;
    }

    public void setProducts(List<Item> product) {
        this.products = product;
    }
}

Item.java

package de.gabriel.mcdonaldsproject.models;

import javax.persistence.*;
public class Item{

    private double count;
    private double price;
    private double order;
    private SubItems subItems;

    public Item(){}
    public Item(double count, double price, double order, SubItems subItems) {
        this.count = count;
        this.price = price;
        this.order = order;
        this.subItems = subItems;
    }

    public double getCount() {
        return count;
    }

    public void setCount(double count) {
        this.count = count;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public double getOrder() {
        return order;
    }

    public void setOrder(double order) {
        this.order = order;
    }

    public SubItems getSubItems() {
        return subItems;
    }

    public void setSubItems(SubItems subItems) {
        this.subItems = subItems;
    }
}

SubItems.java

package de.gabriel.mcdonaldsproject.models;

import javax.persistence.*;
import java.util.List;
public class SubItems {
    private double count;
    private String name;
    private double price;
    private List<String> extraItems;

    public SubItems(){}

    public SubItems(double count, String name, double price, List<String> extraItems) {
        this.count = count;
        this.name = name;
        this.price = price;
        this.extraItems = extraItems;
    }

    public double getCount() {
        return count;
    }

    public void setCount(double count) {
        this.count = count;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public List<String> getExtraItems() {
        return extraItems;
    }

    public void setExtraItems(List<String> extraItems) {
        this.extraItems = extraItems;
    }
}

Does someone have an idea on how to rebuild this structure in Java with JPA so it also gets saved in the database?

CodePudding user response:

If this object orderitem is not going to expand, I would suggest JSON string saving in the database.

OR you can do following mappings:

@OneToMany(mappedBy="order")
public Order(List<Item> products) {
    this.products = products;
}


@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "subitem_id", referencedColumnName = "id")
private SubItems subItems;

CodePudding user response:

Update the following information like this :

@Embeddable
public class Item {
   // .....
   @Embedded
   private SubItems subItems;
   //.......
}

@Embeddable
public class SubItems {
   // .....
   @ElementCollection
   private List<String> extraItems;
   //.......
}


@Entity
@Table(name = "orders", schema = "public")
public class Order implements Serializable {
   //.....
   @ElementCollection
   private List<Item> products;
   //.......
}
  • Related