Jpa:在哪里放置 toString() 自定义方法?

发布于 2024-10-08 09:35:36 字数 408 浏览 7 评论 0原文

我已经使用 Netbeans 从数据库开始自动创建 jpa 实体。 现在我需要在这些实体中插入一些逻辑代码。 例如,首先我需要为每个编写一个自定义的 toString() 方法 jpa实体类。 问题是我必须把代码放在哪里?我知道 jpa 课程可以是 当我在下一次数据库模式更新后再次启动 jpa“向导”时,Netbeans 会覆盖该信息。 所以我想编写一些实体逻辑而不放弃 jpa 自动生成 Netbeans 的功能。 我是否需要使用外部工具,例如合并工具、预处理器 功能或者我需要实现一些继承类或特定模式? 过去我使用过 Apache Cayenne。对于每个数据库实体,它创建两个 java 类。第一个(父级)具有字段实体映射,第二个 (一个空的子类)作为逻辑的容器,永远不会被覆盖 (并且也不再生)。

任何建议将不胜感激。

来自意大利的最诚挚的问候

I have used Netbeans to create automatically jpa entities starting from a db.
Now I need to insert some logic code inside these entities.
For example, first of all I need to write a custom toString() method for every
jpa entity class.
The problem is where I have to put that code ? I know that jpa classes could be
overwritten by Netbeans when I'll launch again the jpa "wizard" after the next db schema update.
So I'd like to write some entity logic without renounce the jpa auto-generation
features of Netbeans.
Does I need to use an external tools like a merging tools, a preprocessor
features or I need to implement some inheritance class or a specific pattern ?
In the past I have used Apache Cayenne. For every db entity it creates two java
classes. The first (the parent), with the fields entity mapping, and the second
(an empty child class) as a container for the logic, which is never overwritten
(and nor regenerated).

Any advices will be appreciated.

Best regards from Italy

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

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

发布评论

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

评论(3

仅冇旳回忆 2024-10-15 09:35:36

从模式生成实体 Bean 的另一种方法是允许 JPA 从实体 Bean 创建模式。我发现它使得更新&维护数据库模式变得更加容易。

An alternative to generating the entity beans from the schema is to allow JPA to create the schema from the entity beans. I've found that it makes updating & maintaining of the DB schema much easier.

余生再见 2024-10-15 09:35:36

我建议您将其作为源代码控制问题来解决。生成您的类,然后根据需要修改它们。一旦您满意,请将结果检查到源代码管理中。当数据库模式更改时,重新生成类。他们将缺少您的修改,但这很好。现在,将代码的本地副本与源代码管理中的内容同步:您将看到两种传出更改:为反映架构中的更改而进行的更改,以及删除您的修改(因为它们已被盲目覆盖)。使用源代码控制工具丢弃后一种更改,并保留前一种更改;本质上,您将新生成的版本与旧的修改版本合并,并保留两者中您想要的位。

我不使用 NetBeans,因此无法详细描述如何执行此操作。在 Eclipse 中,这很简单:重新生成、全选、Team > 。同步,然后在需要的地方进行“从右到左复制当前更改”。

I'd suggest you tackle this as a source control problem. Generate your classes, then modify them as you need. Check the results into source control once you're happy. When the database schema changes, regenerate the classes. They will lack your modifications, but this is fine. Now, synchronise your local copy of the code with what's in source control: you will see two kinds of outgoing changes: changes made to reflect the change in the schema, and deletions of your modifications (because they've been blindly overridden). Use your source control tool to throw away the latter kind of changes, and keep the former; essentially, you are merging the new generated version with the old modified version, and keeping the bits you want from both.

I don't use NetBeans, so i can't describe how to do this in detail. In Eclipse, it's straightforward: regenerate, select all, Team > Synchronize, then go through and do 'copy current change from right to left' wherever you need to.

旧瑾黎汐 2024-10-15 09:35:36

是的,我可以使用修订控制系统将自定义源与自动生成的代码同步。
但是在 jpa 实体类中编写业务逻辑是否正确???

Yes, I could use a revision control system to synchronize the custom source with the auto-generated code.
But is it correct to code business logic inside jpa entity class ???

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