简体   繁体   English

Hibernate:如何在注释中使用级联?

[英]Hibernate: How to use cascade in annotation?

How can I use cascade and annotations in hibernate? 如何在休眠中使用级联和注释?

But I stay with a doubt: 但我怀疑:

I have this situation: 我有这种情况:

public class Package(){
  @OneToOne(cascade=CascadeType.PERSIST)
  private Product product;

  @OneToOne(cascade=CascadeType.PERSIST)
  private User user;
  ..
}

When I try to session.save(package) , an error occurs. 当我尝试session.save(package) ,会发生错误。 I don't want to save product and package. 我不想保存产品和包装。 I just want to initialize and set them into my package object. 我只想初始化并将它们设置到我的包对象中。

Is that possible? 那可能吗?

See the hibernate documentation which is very clear on this issue. 请参阅hibernate文档 ,该文档在此问题上非常清楚。 For instance you could use eg, 例如,您可以使用例如,

@Cascade(CascadeType.PERSIST)
private List<Object> obj;

or 要么

@OneToMany(cascade = CascadeType.PERSIST)
private List<Object> obj;

If you use the hibernate native API , then you should use the annotation of hibernate for cascade and it is : 如果您使用hibernate本机API,那么您应该使用hibernate的注释进行级联,它是:

@Cascade(CascadeType.SAVED_UPDATE)

then you call save() method but with your annotation , you should call the method persist() of the JPA 然后你调用save()方法但是使用你的注释,你应该调用JPA的方法persist()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM