具有复杂关系和外键的 Hibernate 使得加载/保存地狱

发布于 2024-12-27 22:50:54 字数 1456 浏览 2 评论 0原文

我是 Hibernate 的新手,我遇到了一个让我非常头疼的问题:

我有一个使用外键关系的数据库。我首先使用 MySQL InnoDB 表示的所有链接创建了数据库。然后,我从 MySQL Workbench 生成数据库中的所有模型。看起来非常漂亮和方便。

获取用户及其子级确实非常简单快捷;但是添加一个条目变得越来越可怕,我需要加载新对象的所有父对象才能保存它,因为“org.hibernate.PropertyValueException:非空属性引用空值或瞬态值...”。

我的模式很正常,用户有很多照片,照片有斑点和格式,用户有很多收藏夹等等...所以当我想插入新照片时,我需要创建大量的链接对象,这真的很烦人与使用简单的 SQL 查询插入诸如 user_id:15 之类的索引但在选择获取用户对象的条目时相比,更加复杂。

我想知道当获取一个已作为对象加载的对象时,建立复杂数据库关系的好方法是什么:

(User Object)
{
    firstname="John",
    lastname="Doe",
    ...
    photos:[
    {
        format:"jpg",
        url:"http://wifhwofihwofihwf.com",
        ...   
    }],
    favorites:[
    { (user Object)
         firstname:"Steve",
         lastname :"Doe",...
         photos:[...]
    },...
    ]
}

以及如何在获取用户时定义所需响应的深度,因为我们不这样做不想要这样的东西:

(User Object)
    {
        ...
        favorites:[
        { (user Object)
             firstname:"Steve",
             lastname :"Doe",...
             photos:[...],
              {
                  ...
                  favorites:[
                  {
                            ...
                            favorites:[
                            {
                                 LOOONG DEPTH
                            },...
                            ]
                  },...
                  ]
    }

如果您有一些关于如何管理的链接或建议,我将不胜感激,因为我只能找到非常虚拟的休眠示例,这些示例在我的情况下不起作用。

干杯!

I'm quite new in Hibernate, and I'm hurting a problem giving me huge headaches :

I have a database using relationships with foreign keys. I created first my database with all the links represented with MySQL InnoDB. Then, I generated from MySQL Workbench all the models from the database. It looked very nice and convenient.

Getting users and its childs is really easy and fast; BUT adding an entry is getting horrible, I need to load all the parents of the new object to be able to save it because of the "org.hibernate.PropertyValueException: not-null property references a null or transient value...".

My schema is quite normal, a user has many photos, photos have blobs and formats, user has many favorites etc... So when I want to insert a new Photo, I need to create a HUGE amount of linked object which is really annoying and complex, compare to using a simple SQL query inserting indexes like user_id:15 but when selecting an entry getting a User Object.

I wonder what is the good way of making relationships of complex databases, when getting an object you have loaded as objects its objects like that :

(User Object)
{
    firstname="John",
    lastname="Doe",
    ...
    photos:[
    {
        format:"jpg",
        url:"http://wifhwofihwofihwf.com",
        ...   
    }],
    favorites:[
    { (user Object)
         firstname:"Steve",
         lastname :"Doe",...
         photos:[...]
    },...
    ]
}

And how is it possible to define the depth of the required response when getting a User, because we don't want something like :

(User Object)
    {
        ...
        favorites:[
        { (user Object)
             firstname:"Steve",
             lastname :"Doe",...
             photos:[...],
              {
                  ...
                  favorites:[
                  {
                            ...
                            favorites:[
                            {
                                 LOOONG DEPTH
                            },...
                            ]
                  },...
                  ]
    }

if you have some links or advices about how to manage that I would be thankful, because I can find only very dummy examples of hibernate which are not working in my case.

Cheers !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谎言 2025-01-03 22:50:54

不要寻找例子。阅读参考手册

要限制实体树的深度,请将关联声明为惰性关联。这是 toMany 关联的默认设置。

@ManyToOne(lazy = true)
private User owner;

要保存新照片,如果这张新照片属于某个用户,您只需获取该用户的引用即可。无需加载该用户的照片,或任何其他间接依赖。而且你甚至不需要访问数据库:

Photo photo = new Photo();
photo.setOwner((User) session.load(User.class, userId));
session.persist(photo);

我不明白它怎么这么困难。也许您应该详细说明一个保存新照片的代码示例。

Don't look for examples. Read the reference manual.

To limit the depth of the tree of entities, declare the associations as lazy. This is the default for toMany associations.

@ManyToOne(lazy = true)
private User owner;

To save a new Photo, if this new Photo belongs to a User, you just have to get a reference to this user. No need to load the photos of this user, or any other indirect dependency. And you don't even need to hit the database:

Photo photo = new Photo();
photo.setOwner((User) session.load(User.class, userId));
session.persist(photo);

I don't see how it is so difficult. Maybe you should elaborate with an example of you code persisting a new Photo.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文