持久化类中存在多个参数构造函数有什么原因吗?

发布于 2024-12-22 19:36:26 字数 321 浏览 3 评论 0原文

我正在开发一个继承的 Java Web 应用程序,该应用程序使用 Spring 和 Hibernate。该应用程序包含使用 hbm2java 自动生成的持久性类。这些类中包含具有多个参数的构造函数(以及典型的默认构造函数)。这些非默认构造函数不会被任何其他方法直接访问。也没有任何明确的 spring 定义来调用这些类,所以看起来它是死代码......

现在我知道这个问题非常模糊,但是 Hibernate 通常会通过反射调用这些构造函数是否有任何正常的原因?我知道它使用 Class.getInstance() 调用默认构造函数来创建对象的新实例,但仅此而已......

I am working on a Java web application that I inherited that uses Spring and Hibernate. This application contains persistence classes that were automatically generated using hbm2java. Included in these classes were constructors with multiple parameters (as well as your typical default constructor). These non-default constructors are not being directly accessed by any other methods. Nor is there any explicit spring definition for calling these classes, so it appears that it is dead code...

Now I know this question is hideously vague, but would there be any normal reason where Hibernate would normally invoke these constructors through reflection? I know it uses Class<T>.getInstance() to invoke the default constructor to create a new instance of the object, but that's about it...

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

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

发布评论

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

评论(2

樱&纷飞 2024-12-29 19:36:26

不,这些不存在,因为休眠会使用它们。

当您实例化和初始化实体类时,这种类型的构造函数通常很有用。与使用默认构造函数和调用多个 setter 不同,使用带有多个参数的构造函数会更容易。

No, those are not there because the hibernate will use them.

That type of constructors are often useful when you instantiate and initialize the entity class. Instead of using the default constructor and calling multiple setters, it's easier to use a constructor that takes multiple parameters.

纵山崖 2024-12-29 19:36:26

看看你的数据库。通常,hibernate 会为数据库中的所有列创建构造函数,然后为所有必填字段创建另一个构造函数。

Have a look at your db. Normally hibernate creates constructors for all the columns in your db and then another constructor for all the mandatory fields.

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