java hibernate entity:允许通过id和object本身来设置相关的对象 [英] java hibernate entity: allow to set related object both by id and object itself

查看:350
本文介绍了java hibernate entity:允许通过id和object本身来设置相关的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  @Entity 
@Table我有一个下面的Java类,它也是一个Hibernate实体: (name =category)
public class Category {

@ManyToOne
@JoinColumn(name =parent_id)
private类别父类;

public Category getParent(){
return parent;
}

public void setParent(Category parent){
this.parent = parent;





类别表示类别树中的一个节点。我正在实现一个允许CRUD类别的web服务。例如,接口有能力创建一个类别树节点,并将类别id作为参数传递。



我只想创建一个新的Category对象,并坚持它没有获取父对象而进入数据库。我的数据提供者类看起来像这样:
$ b $ pre $ public void createCategory(int parent_id,String name,CategoryType type){
Category category = new Category();
category.setName(name);
// category.setParent(?); < - 这里没有这个对象
// category.setParentId(id); < - 但我有id
category.setType(type);
this.categoryDao.save(category);





$ b我的问题是:我能做些什么来创建一个新的Category对象parent_id设置如果假设我不会调用hibernate来获取父母(这将是愚蠢的)?我可以为Category类提供setParentId / getParentId方法吗? Hibernate提供了一个名为(相当容易混淆) Session.load的方法()



Session.load()返回一个懒代理使用给定的标识符而不查询数据库(如果具有给定标识符的对象已经加载到当前 Session 中,则它自己返回一个对象)。

您可以使用该代理来初始化正在保存的实体中的关系:

  category.setParent session.load(Category.class,parent_id)); 

请注意,此代码不会检查是否存在 Category 给定的ID。但是,如果在数据库模式中存在外键约束,则在传入无效标识时,您将收到约束违例错误。



JPA等同于此方法称为 EntityManager.getReference()


I've got a following Java class which is also a Hibernate entity:

@Entity
@Table(name = "category")
public class Category {

    @ManyToOne
    @JoinColumn(name="parent_id")
    private Category parent;

    public Category getParent() {
        return parent;
    }

    public void setParent(Category parent) {
        this.parent = parent;
    }

The category represents a node in a category tree. I'm implementing a webservice which allows to CRUD categories. For instance, the interface has the ability to create a category tree node and it passes the category id as a parameter.

I want just to create a new Category object and persist it into the database without fetching the parent object. My data provider class looks like this:

public void createCategory(int parent_id, String name, CategoryType type) {
    Category category = new Category();
    category.setName(name);
    // category.setParent(?); <- I don't have this object here
    // category.setParentId(id); <- but I do have the id
    category.setType(type);
    this.categoryDao.save(category);
}

My question is: what can I do to create a new Category object with the parent_id set if assuming I won't call hibernate to fetch the parent for me (this would be stupid)? Can I provide a setParentId/getParentId method for Category class? What hibernate annotations would it have?

解决方案

Hibernate provides a method called (quite confusingly) Session.load() for this scenario.

Session.load() returns a lazy proxy with the given identifier without querying the database (if object with the given identifier is already loaded in the current Session, it returns an object itself).

You can use that proxy to initialize relationships in your entities being saved:

category.setParent(session.load(Category.class, parent_id));

Note that this code doesn't check existence of Category with the given id. However, if you have a foreign key constraint in your DB schema, you'll get a constraint violation error when invalid id is passed in.

JPA equivalent of this method is called EntityManager.getReference().

这篇关于java hibernate entity:允许通过id和object本身来设置相关的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆