Spring Hibernate - FindByObject CrudRepository [英] Spring Hibernate - FindByObject CrudRepository

查看:146
本文介绍了Spring Hibernate - FindByObject CrudRepository的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能(如果是这样,如何)在CrudRepository接口中添加自定义方法?

我有一个名为Foobar的实体类,具有以下DAO。为了这个问题 - 实体类可以有任何数量的字段/列。

  package com.bar 
import org.springframework.data.repository.CrudRepository;
import com.bar.Foobar
public interface FoobarCrudRepo扩展CrudRepository< Foobar,Long> {
public Foobar findByObject(Foobar JObj);



$ b $ p
$ b

如果您移除该自定义方法但是会抛出错误添加它(如上)。我对这篇文章的理解(更新或SaveorUpdate在CRUDRespository中,是否有任何可用的选项)是我只需要添加一个额外的方法到接口(如示例中所示),JPARepo将负责其余的。但是,我收到下面的错误。我的想法 - 对这个自定义课程不太了解。



这是我的错误:

 嵌套异常是org.springframework。 beans.factory.BeanCreationException:创建名为'Foobar'的Bean时出错:init方法的调用失败;嵌套的异常是org.springframework.data.mapping.PropertyReferenceException:找不到Foobar类型的属性对象! 

为什么我需要findbyObject(),你可能会问这个问题?这样我就不会通过检查数据库来查看是否已添加对象并执行检查,从而将重复的数据行插入到数据库中。



任何建议都会有所帮助,谢谢。

解决方案

FindByObject 不寻找对象,对于名为 Object Foobar 类中的字段。像其他人提到的一样,您可以检查对象的其他字段以查看它是否已经存在。例如,如果您的 Foobar 类有字符串名称 >和字符串电子邮件字段,您可以创建一个方法,如 public Foobar findByNameAndEmail(String name,String email){} ,如果数据库中有符合这些条件的记录,这将返回一个Foobar对象。



所有查询方法都是复制SQL,所以问问自己,是否可以在SQL中通过对象(记录)查找?不,因为这没有意义。你的查询会返回一个记录(对象),在某些条件满足的情况下。

Is it possible (if so, how) would one add custom methods in the CrudRepository interface?

I have the following DAO with a entity class called, Foobar. For the sake of this question - the entity class can have any number of fields/columns.

package com.bar
import org.springframework.data.repository.CrudRepository;
import com.bar.Foobar
public interface FoobarCrudRepo extends CrudRepository<Foobar, Long> {
    public Foobar findByObject(Foobar JObj);
}

This code works fine if you remove that custom method but throws an error if I add it (like above). My understanding from this post (Update or SaveorUpdate in CRUDRespository, Is there any options available) was that I just needed to add an additional method to the interface (as shown in the example) and the JPARepo will take care of the rest. However, I'm getting the error below. My thinking - it doesn't know much about this custom class.

Here's my error:

nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'Foobar': Invocation of init method failed; nested exception is org.springframework.data.mapping.PropertyReferenceException: No property object found for type Foobar!

Why would I want a findbyObject() you might ask? So that I don't insert duplicate rows of data into the DB by checking the DB to see if the object has already been added and performing a check.

Any suggestions would help, thanks.

解决方案

FindByObject doesn't look for an object, it literally looks for a field in your Foobar class named Object. Like others have mentioned, you can check other fields of the object to see if it already exists.

For example, if your Foobar class has String name and String email fields, you could create a method like public Foobar findByNameAndEmail(String name, String email) {}, and this would return a Foobar object if there was a record in the database that met these criteria.

All query methods do is replicate SQL, so ask yourself, could you "find by object (record)" in SQL? No, because that makes no sense. Your query would return a record (object) WHERE certain conditions are met.

这篇关于Spring Hibernate - FindByObject CrudRepository的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆