本文介绍了Hibernate在外键字段中插入空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有2个简单的域对象,如下所示..使用MYSQL DB.
I have 2 simple domain objects as follows..with MYSQL DB.
@Entity
@Table(name="Product")
public class Product {
@Id
@Column(name="productId")
@GeneratedValue
protected int productId;
@Column(name="Product_Name")
protected String name;
@OneToMany(cascade = javax.persistence.CascadeType.ALL,mappedBy="product")
protected List<ProductOption> productoption = new ArrayList<ProductOption>();
和第二个对象
@Entity
@Table(name="ProductOption")
public class ProductOption{
/**
*
*/
@Id
@Column(name="product_option_id")
@GeneratedValue
protected int productOptionId;
//@JoinColumn(name="productId")
@ManyToOne()
protected Product product;
@Column(name="ProductTopping")
private String topping;
和我的主要方法.
public class Createschema {
public static void main(String[] args) {
Product product = new Product();
product.setName("Coffee");
ProductOption po2 = new ProductOption();
po2.setTopping("barbeque");
ProductOption po = new ProductOption();
po.setTopping("whipcream");
ProductOption po1 = new ProductOption();
po1.setTopping("honeymustard");
List<ProductOption> productoptions = new ArrayList<ProductOption>();
productoptions.add(po1);
productoptions.add(po2);
productoptions.add(po);
System.out.println("schema!");
Configuration configuration = new Configuration().configure();
StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());
SessionFactory factory = configuration.buildSessionFactory(builder.build());
Session session = factory.openSession();
session.beginTransaction();
product.setProductoption(productoptions);
session.save(product);
session.getTransaction().commit();
session.close();
System.out.println(product.getName());
for(int i=0;i<product.getProductoption().size();i++){
System.out.println(product.getProductOptionsAsListOfStrings().get(i));
System.out.println(product.getProductoption().get(i).getTopping());
}
}
}
出于某种原因,hibernate在productoptions的外键字段中插入了空值.我尝试更改级联样式,生成策略,但没有任何效果.使nullable = false引发error.插入了数据,但在productoptions表中,foreign键字段为null.请帮助
For some reason hibernate is inserting null values in foreign key field of productoptions.I tried changing cascading styles, generation strategies, but nothing worked. Making nullable=false throws error . Data gets inserted but in the productoptions table foriegn key field is null.Please help
推荐答案
在Product
更改中
@OneToMany(mappedBy="product")
List<ProductOption> productoption;
在ProductOption
更改中
@ManyToOne
@JoinColumn(name="productId")
private Product product;
最后是Createschema
session.beginTransaction();
Product product = new Product();
product.setName("Coffee");
session.save(product);
ProductOption po2 = new ProductOption();
po2.setTopping("barbeque");
po2.setProduct(product)
ProductOption po = new ProductOption();
po.setTopping("whipcream");
po2.setProduct(product)
ProductOption po1 = new ProductOption();
po1.setTopping("honeymustard");
po2.setProduct(product)
session.save(po2);
session.save(po);
session.save(po1);
session.getTransaction().commit();
session.close();
这篇关于Hibernate在外键字段中插入空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!