在hibenate / jpa实体类中定义save方法是一个很好的约定吗?(Is it a good convention to define save method inside hibernate/jpa entity class?)

我有一个hibernate实体/模型类,如下所示

class User{ private Integer id, Private String name, private String description //getters and setters public void Save(this){ } }

将此类声明中的save方法与setter和getter一起定义是一个很好的约定吗? 我们是否有使用这样的好处,例如避免重复记录?

I have a hibernate entity/model class as given below

class User{ private Integer id, Private String name, private String description //getters and setters public void Save(this){ } }

Is it a good convention to define save method inside this class declaration along with setters and getters? Do we have any benefit of using like this such as avoiding duplicate records?

最满意答案

不,这不是一个好的约定。 通常你有一个名为DAO(数据访问对象)的特殊层,它关心保存/获取/删除......你的实体。 域模型(即您的实体)仅表示数据,并且存在与其一起使用的DAO层。 使用JPA这样强大的规范,您可以将DAO层与业务逻辑层(EJB / Beans / CDI)合并,但这可能是另一个讨论的主题。 当然,您可以在实体中进行一些验证,或者例如没有持久化的方法getLoggingString(),但返回要记录的实体的String版本。

PS:您编写的方法对该签名没有多大意义:您需要一个public User save()方法来保存自己,或者您需要一个static public User save(User userToSave) 。

一些参考:

DAO 您是否应该有一个单独的DAO层(即不与您的业务层合并)?

No, that is not a good convention. Usually you have a special layer called DAO (Data Access Object) that cares about saving/fetching/deleting... your entities. The Domain Models (i.e your entities) represent only data, and there is the DAO layer that plays with them. With such a powerful specification as JPA you could merge your DAO layer with your Business Logic layer (EJB/Beans/CDI), but that could be the topic for another discussion. Of course you could have some validation things in your entity, or e.g a method getLoggingString() that is not persisted, but returns a String version of the entity to be logged.

PS: the method that you wrote makes little sense with that signature: you need either a public User save() method that saves itself or you need a static public User save(User userToSave).

Some references:

DAO Should you have a separate DAO layer (i.e not merged with your business layer)?在hibenate / jpa实体类中定义save方法是一个很好的约定吗?(Is it a good convention to define save method inside hibernate/jpa entity class?)

我有一个hibernate实体/模型类,如下所示

class User{ private Integer id, Private String name, private String description //getters and setters public void Save(this){ } }

将此类声明中的save方法与setter和getter一起定义是一个很好的约定吗? 我们是否有使用这样的好处,例如避免重复记录?

I have a hibernate entity/model class as given below

class User{ private Integer id, Private String name, private String description //getters and setters public void Save(this){ } }

Is it a good convention to define save method inside this class declaration along with setters and getters? Do we have any benefit of using like this such as avoiding duplicate records?

最满意答案

不,这不是一个好的约定。 通常你有一个名为DAO(数据访问对象)的特殊层,它关心保存/获取/删除......你的实体。 域模型(即您的实体)仅表示数据,并且存在与其一起使用的DAO层。 使用JPA这样强大的规范,您可以将DAO层与业务逻辑层(EJB / Beans / CDI)合并,但这可能是另一个讨论的主题。 当然,您可以在实体中进行一些验证,或者例如没有持久化的方法getLoggingString(),但返回要记录的实体的String版本。

PS:您编写的方法对该签名没有多大意义:您需要一个public User save()方法来保存自己,或者您需要一个static public User save(User userToSave) 。

一些参考:

DAO 您是否应该有一个单独的DAO层(即不与您的业务层合并)?

No, that is not a good convention. Usually you have a special layer called DAO (Data Access Object) that cares about saving/fetching/deleting... your entities. The Domain Models (i.e your entities) represent only data, and there is the DAO layer that plays with them. With such a powerful specification as JPA you could merge your DAO layer with your Business Logic layer (EJB/Beans/CDI), but that could be the topic for another discussion. Of course you could have some validation things in your entity, or e.g a method getLoggingString() that is not persisted, but returns a String version of the entity to be logged.

PS: the method that you wrote makes little sense with that signature: you need either a public User save() method that saves itself or you need a static public User save(User userToSave).

Some references:

DAO Should you have a separate DAO layer (i.e not merged with your business layer)?