为什么实体没有存储在数据库中?

时间:2019-02-16 16:21:21

标签: java spring hibernate jpa kotlin

我有两个相关的面孔。保存到数据库时,仅成功保存了一个对象。带有外键的第二个不保存。具有外键的第二个对象是列表对象,请帮助。

在这里我添加了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);

服务类别:

    @Repository
    @Transactional
    public class MyServiceClass {

        @Autowired
        @Qualifier(value = "sessionFactory")
        SessionFactory session;



       public boolean createNewOrder(MyOrder myOrder){
            session.getCurrentSession().save(myOrder);
            return true;
        }

}

模式:

CREATE TABLE `myorder` (
  `myOrder_id` int(11) NOT NULL AUTO_INCREMENT,
  `date` varchar(255) DEFAULT NULL,
  `status` varchar(255) DEFAULT NULL,
  `myorder_company_id` int(11) DEFAULT NULL,
  `myorder_courier_id` int(11) DEFAULT NULL,
  PRIMARY KEY (`myOrder_id`),
  KEY `FK_q6alh4tjuhyudpq770duori5h` (`myorder_company_id`),
  KEY `FK_lcjnn2172xke45f2gmsiyvtep` (`myorder_courier_id`),
  CONSTRAINT `FK_lcjnn2172xke45f2gmsiyvtep` FOREIGN KEY (`myorder_courier_id`) REFERENCES `courier` (`id`),
  CONSTRAINT `FK_q6alh4tjuhyudpq770duori5h` FOREIGN KEY (`myorder_company_id`) REFERENCES `company` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;


CREATE TABLE `products` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `amount` varchar(255) DEFAULT NULL,
  `consignation` bit(1) NOT NULL,
  `debt` bit(1) NOT NULL,
  `description` varchar(255) DEFAULT NULL,
  `name` varchar(255) DEFAULT NULL,
  `photo` varchar(255) DEFAULT NULL,
  `price` varchar(255) DEFAULT NULL,
  `rec_price` varchar(255) DEFAULT NULL,
  `returned` bit(1) NOT NULL,
  `weight` varchar(255) DEFAULT NULL,
  `product_categories_id` int(11) DEFAULT NULL,
  `product_company_id` int(11) DEFAULT NULL,
  `product_myorder_id` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `FK_ovaxmqou7gnwgchnhbkp9tq1p` (`product_categories_id`),
  KEY `FK_kwu6gyind0d2bthu19fcrkkcf` (`product_company_id`),
  KEY `FK_jeh86q0ligae6ggjlq9lq89y5` (`product_myorder_id`),
  CONSTRAINT `FK_jeh86q0ligae6ggjlq9lq89y5` FOREIGN KEY (`product_myorder_id`) REFERENCES `myorder` (`myOrder_id`),
  CONSTRAINT `FK_kwu6gyind0d2bthu19fcrkkcf` FOREIGN KEY (`product_company_id`) REFERENCES `company` (`id`),
  CONSTRAINT `FK_ovaxmqou7gnwgchnhbkp9tq1p` FOREIGN KEY (`product_categories_id`) REFERENCES `categories` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8;

2 个答案:

答案 0 :(得分:2)

您应该考虑以下映射:

  • 使用Long for id列,而不是int-int太小
  • 双向映射可能更好
  • 具有用于添加/删除子级的实用程序方法(如下代码所示)
  • 在Entity类中初始化您的集合,以避免空值

示例:

@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
}

答案 1 :(得分:1)

您所做的更改并未从父级实体级联到子级实体,您需要将其添加到映射中:

@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;

如果列名错误,请尝试以下操作:

@ManyToOne
@JoinColumn(name = "myorder_id")
private MyOrder myOrder;