第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

為什么實(shí)體沒有存儲(chǔ)在數(shù)據(jù)庫中?

為什么實(shí)體沒有存儲(chǔ)在數(shù)據(jù)庫中?

暮色呼如 2022-06-23 17:37:15
我有兩張相關(guān)的面孔。保存到數(shù)據(jù)庫時(shí),只有一個(gè)對(duì)象被成功保存。第二個(gè)帶外鍵的不保存。第二個(gè)有外鍵的是 List objects Please help。在這里我添加了cascade = CascadeType.ALL,但它仍然沒有工作@Entity@Table(name = "MyOrder")public class MyOrder {    @Id    @GeneratedValue(strategy = GenerationType.AUTO)    private int id;    @OneToMany(mappedBy = "myOrder")    private List<Products> productses;我添加了 fetch = Fetch.LAZY,這也沒有幫助。@Entity@Table(name = "Products")public class Products {    @Id    @GeneratedValue(strategy = GenerationType.AUTO)    private int id;    @ManyToOne    @JoinColumn(name = "product_myorder_id")    private MyOrder myOrder;在控制器中我填充它:        MyOrder myOrder = new MyOrder();        List<Products2> products2s =  object.getProductses2();        List<Products> productses =  new ArrayList<Products>();        for(int i =0;i<products2s.size();i++){            Products2 products2 = products2s.get(i);            Products products = new Products();            products.setName(products2.getName());            products.setWeight(products2.getWeight());            products.setAmount(products2.getAmount());            products.setPrice(products2.getPrice());            productses.add(products);        }        myOrder.setProductses(productses);        //saving        serviceClass.createNewOrder(myOrder);服務(wù)等級(jí):    @Repository    @Transactional    public class MyServiceClass {        @Autowired        @Qualifier(value = "sessionFactory")        SessionFactory session;       public boolean createNewOrder(MyOrder myOrder){            session.getCurrentSession().save(myOrder);            return true;        }}
查看完整描述

2 回答

?
慕尼黑5688855

TA貢獻(xiàn)1848條經(jīng)驗(yàn) 獲得超2個(gè)贊

您應(yīng)該考慮以下映射:

  • 將 Long 用于 id 列,而不是 int - int 太小

  • 雙向映射可能更好

  • 有用于添加/刪除孩子的實(shí)用方法(如下面的代碼)

  • 在實(shí)體類中初始化您的集合以避免空值

例子:

@Entity(name = "MyOrder")

@Table(name = "myorder")

public class MyOrder {

    @Id

    @GeneratedValue

    private Long id;


    @OneToMany(mappedBy = "myorder", cascade = CascadeType.ALL, orphanRemoval = true)

    private List<Product> products = new ArrayList<>();


    //Constructors, getters, setters etc.


    public void addProduct(Product product) {

        products.add(product);

        product.setMyOrder(this);

    }


    public void removeProduct(Product product) {

        products.remove(product);

        product.setMyOrder(null);

    }

}


@Entity(name = "Product")

@Table(name = "product")

public class Product {


    @Id

    @GeneratedValue

    private Long id;


    @ManyToOne(fetch = FetchType.LAZY)

    @JoinColumn(name = "myorder_id")

    private MyOrder myOrder;


    //Constructors, getters and setters


    //equals and hashCode

}


查看完整回答
反對(duì) 回復(fù) 2022-06-23
?
Qyouu

TA貢獻(xiàn)1786條經(jīng)驗(yàn) 獲得超11個(gè)贊

您的更改沒有從父實(shí)體級(jí)聯(lián)到子實(shí)體,您需要將其添加到您的映射中:


@Entity

@Table(name = "MyOrder")

public class MyOrder {

    @Id

    @GeneratedValue(strategy = GenerationType.AUTO)

    private int id;


    @OneToMany(mappedBy = "myOrder", cascade = CascadeType.All)

    private List<Products> productses;

使用列名錯(cuò)誤試試這個(gè):


@ManyToOne

@JoinColumn(name = "myorder_id")

private MyOrder myOrder;


查看完整回答
反對(duì) 回復(fù) 2022-06-23
  • 2 回答
  • 0 關(guān)注
  • 155 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)