[英]How to persist Entity object which contains another Entity Objects to the database?
I have a problem with my function which is supposed to save data to database, function creates a new object which includes other objects. 我的函数存在问题,该函数应该将数据保存到数据库,函数会创建一个包含其他对象的新对象。 I am able to save and fetch objects which contain only primitive data types and Strings to Database , so the database and system works in this case.
我能够将仅包含原始数据类型和字符串的对象保存并提取到Database,因此在这种情况下,数据库和系统都可以使用。 I am using JavaEE and EntityManager (persist).
我正在使用JavaEE和EntityManager(持久性)。 Any help?
有什么帮助吗?
You can define a cascade type on the parent entity. 您可以在父实体上定义级联类型。 Have a look on the Java EE tutorial ( http://docs.oracle.com/javaee/6/tutorial/doc/bnbqa.html#bnbqm )
看看Java EE教程( http://docs.oracle.com/javaee/6/tutorial/doc/bnbqa.html#bnbqm )
see the principal below 见下面的校长
@Entity
public class Customer {
@OneToMany(cascade=CascadeType.ALL, mappedBy="customer")
public Set<Order> getOrders() {
return orders;
}
...
}
@Entity
public class Order {
@OneToOne
Customer customer;
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.