使用 hibernate 的服务的最佳实践
我正在使用 Hibernate,并且遇到流程/最佳实践问题。例如,假设我们有 2 个实体“卡车”和“车轮”。车轮有 2 个属性wheelid 和wheelmanufacturer。因此,在我的应用程序中,用户可以创建一辆卡车,他们可以选择数据库中存在的各种车轮或创建自己的车轮。
我的问题是,当我从前端收到卡车对象时,如何确保数据库中是否存在车轮,则使用数据库副本,如果数据库中不存在,则添加它。这是我的服务现在如何工作的一个基本示例。
public Truck saveTruck( Truck truck )
{
//Yes, this is a two-wheel truck =)
Wheel frontWheel = truck.getFrontWheel();
if( frontWheel != null && frontWheel.getWheelId() != null )
{
Wheel dbWheel = wheelDao.getWheelById( frontWheel.getWheelId() );
if( dbWheel != null )
{
truck.setFrontWheel(dbWheel );
}
}
}
这样做的动机是我不希望用户能够覆盖数据库中的对象,但是他们需要能够编写自定义对象。上面的代码可以工作,但是如果您有一辆具有 10 个属性的卡车,那么它就变得非常多余。我想知道其他人如何解决这个问题。
谢谢
I'm using Hibernate and I have a process/best practices issue. For an example, lets assume we have 2 entities Truck and Wheel. The wheel has 2 properties wheelid and wheelmanufacturer. So in my application, the user can create a truck, they can select various wheels that exists in the database or create their own wheel.
My question is when I receive a truck object from the front-end , how do I make sure that if the wheel does exist in the database, the db copy is used and if it doesn't exist in the database, it is added. Here is a rudimentary example of how my services work now
public Truck saveTruck( Truck truck )
{
//Yes, this is a two-wheel truck =)
Wheel frontWheel = truck.getFrontWheel();
if( frontWheel != null && frontWheel.getWheelId() != null )
{
Wheel dbWheel = wheelDao.getWheelById( frontWheel.getWheelId() );
if( dbWheel != null )
{
truck.setFrontWheel(dbWheel );
}
}
}
The motivation for this is that I don't want users to be able to overwrite objects in the database, however they need to be able to write custom ones. The above code works, but it becomes very redundant if you have a truck with say 10 properties. I was wondering how others solve this problem.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这正是
merge()
操作的作用。也就是说,您的代码可以替换为以下代码:
其中
wheelDao.merge()
提供对Session.merge()
的访问。如果您的
Truck
有许多要合并的属性,您可以将它们配置为级联merge()
操作,并将Truck
合并为一个整体:另请参阅:
That's exactly what the
merge()
operation do.That is, your code can be replaced by the following:
where
wheelDao.merge()
provides access toSession.merge()
.If your
Truck
has many properties to be merged, you can configure them to cascade amerge()
operation and merge theTruck
as a whole:See also:
我在应用程序的用户界面部分解决了此类问题。我有一个组件来显示值对象,例如轮子,如果用户无法从现有轮子中找到请求的轮子,则用户可以通过该组件创建新的轮子。
I solve these kind of problems at user interface part of the application. I have a component to display value objects such as wheel, if the user could not find the requested wheel from existing wheels, the user can create a new wheel by means of the component.
我将通过将 GUI 与后端分离来实现这一点。
GUI 将使用 TruckDTO,它几乎就是 Truck 对象,但它包含 WeelManufactuer 字符串而不是 Weel。
GUI 控制器接收 TruckDTO 并将其转发给某个服务。
该服务在数据库中搜索制造商的 Weel。如果没有这样的威尔,它就会创造它。然后它使用 weel(新的或现有的)和 TruckDTO 创建卡车对象。
更新卡车也是如此。 (当然,您必须更改它,而不是创建新卡车)。
我将处理该问题的逻辑不在界面部分中,而是在应用程序的服务部分中。因为所以任何 GUI 实现都不会忘记实现正确的处理。
I would implement this by seperating the GUI from the Backend.
The GUI would use a TruckDTO which is almost the Truck object, but instead of a Weel it contains a WeelManufactuer String.
The GUI Controller recive the TruckDTO and forward it to some Service.
The service, seach for a Weel with the manufactuer in the DB. If there is no such weel it creates it. Then it uses the weel (new or existing one) and the TruckDTO to create the Truck Object.
The same is for updating the Truck. (Of course, instead of create a new Truck, you have to change it).
I put the logic to handle that not in the interface part, but in the service part of the application. Because so no GUI implementation can forget to implement the correct handling.