标签: spring-data-jpa

尝试使用Spring Data JPA创建自定义存储库时,找不到类型错误的属性

我有一个Media实体,它有一些用户上传文件的基本字段.为了保存上传文件的字节,我想创建一个包含该功能的自定义存储库.按照Spring文档中的步骤,我创建了一个如下所示的界面:

public interface MediaBytesRepository
{
    public byte[] getBytes(Media media) throws IOException;
    public void saveBytes(Media media, byte[] bytes) throws IOException;
    public void appendBytes(Media media, byte[] bytes) throws IOException;
    public void deleteBytes(Media media) throws IOException;
    public boolean bytesExist(Media media) throws IOException;
}
Run Code Online (Sandbox Code Playgroud)

然后我提供了一个名为的接口的实现 MediaBytesRepositoryImpl

有了这个,我创建了以下界面:

public interface MediaRepository extends JpaRepository<Media, Long>, MediaBytesRepository
{
}
Run Code Online (Sandbox Code Playgroud)

现在,当我启动服务器时,我得到以下堆栈跟踪:

SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mediaRepository': FactoryBean threw exception on object creation; …
Run Code Online (Sandbox Code Playgroud)

java spring repository spring-data-jpa

39
推荐指数
2
解决办法
4万
查看次数

查询方法中的Spring Data可选参数

我想在存储库层中编写一些查询方法.此方法必须忽略null参数.例如:

List<Foo> findByBarAndGoo(Bar barParam, @optional Goo gooParam);
Run Code Online (Sandbox Code Playgroud)

此方法必须通过以下条件返回Foo:

bar == barParam && goo == gooParam;
Run Code Online (Sandbox Code Playgroud)

如果gooParam不为null.如果gooParam为null,则条件更改为:

bar == barParam;
Run Code Online (Sandbox Code Playgroud)

有什么解决方案吗?有人能帮我吗?

spring hibernate jpql spring-data spring-data-jpa

39
推荐指数
6
解决办法
2万
查看次数

为什么我们必须在Data Jpa中对查询使用@Modifying注释

例如,我的CRUD界面中有一个方法可以从数据库中删除用户:

public interface CrudUserRepository extends JpaRepository<User, Integer> {

    @Transactional
    @Modifying
    @Query("DELETE FROM User u WHERE u.id=:id")
    int delete(@Param("id") int id, @Param("userId") int userId);
}
Run Code Online (Sandbox Code Playgroud)

此方法仅适用于注释@Modifying.但是这里的注释需要什么?为什么不能分析查询并理解它是一个修改查询?

java spring spring-annotations spring-data-jpa

39
推荐指数
3
解决办法
2万
查看次数

使用@Query,使用hibernate更新spring数据jpa中的boolean值

我有spring-data和hibernate配置并运行.我可以使用spring-data保存记录但由于某种原因,我无法运行将更新表中所有布尔字段的查询.

我试过这个:

@Query("update Content v set v.published = false where v.division = :division and v.section = :section")
void unPublishContent(@Param("division") String division, 
                         @Param("section") String section);
Run Code Online (Sandbox Code Playgroud)

我也试过这个:

 @Query("update Content v set v.published = 0 where v.division = :division and v.section = :section")
void unPublishContent(@Param("division") String division, 
                         @Param("section") String section);
Run Code Online (Sandbox Code Playgroud)

参数division和section正在实现,但表中没有变化.

ps我也在使用mysql数据库.

hibernate spring-data spring-data-jpa

38
推荐指数
3
解决办法
4万
查看次数

Spring引用JPA在TABLE中插入大写名称和Hibernate

我有一个表实体映射为:

@Entity
public class ItemsToRegister implements Serializable{

@Id
@Column(name = "ID_ITEM_TO_REGISTER")
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
.....
Run Code Online (Sandbox Code Playgroud)

当我尝试在数据库中插入新记录时,表名被小写翻译为:items_to_register,但我的表名是ITEMS_TO_REGISTER如何在不更改MySql配置的情况下修复我的问题?(my.cnf中)

我在我的application.properties文件中:

spring.jpa.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
spring.jpa.hibernate.naming_strategy = org.hibernate.cfg.ImprovedNamingStrategy
Run Code Online (Sandbox Code Playgroud)

java mysql hibernate jpa spring-data-jpa

38
推荐指数
5
解决办法
4万
查看次数

ImprovedNamingStrategy不再适用于Hibernate 5

我有简单的spring-jpa配置,我配置了Hibernate ImprovedNamingStrategy.这意味着如果我的实体类有一个变量userName,那么Hibernate应该将它转换user_name为查询数据库.但是在我升级到Hibernate 5后,这个命名转换停止了工作.我收到错误:

错误:"字段列表"中的未知列'user0_.userName'

这是我的Hibernate配置:

@Configuration
@EnableJpaRepositories("com.springJpa.repository")
@EnableTransactionManagement
public class DataConfig {

    @Bean
    public DataSource dataSource(){
        DriverManagerDataSource ds = new DriverManagerDataSource();
        ds.setDriverClassName("com.mysql.jdbc.Driver");
        ds.setUrl("jdbc:mysql://localhost:3306/test");
        ds.setUsername("root");
        ds.setPassword("admin");
        return ds;
    }


    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory(){ 

        HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        vendorAdapter.setShowSql(Boolean.TRUE);
        vendorAdapter.setDatabase(Database.MYSQL);

        LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
        factory.setJpaVendorAdapter(vendorAdapter);
        factory.setDataSource(dataSource());
        factory.setPackagesToScan("com.springJpa.entity");


        Properties jpaProperties = new Properties();

        jpaProperties.put("hibernate.ejb.naming_strategy","org.hibernate.cfg.ImprovedNamingStrategy");
        jpaProperties.put("hibernate.dialect","org.hibernate.dialect.MySQL5InnoDBDialect");

        factory.setJpaProperties(jpaProperties);
        factory.afterPropertiesSet();
        return factory;
    }

    @Bean
    public SharedEntityManagerBean entityManager() {
        SharedEntityManagerBean entityManager = new SharedEntityManagerBean();
        entityManager.setEntityManagerFactory(entityManagerFactory().getObject());
        return entityManager;
    }



    @Bean
    public …
Run Code Online (Sandbox Code Playgroud)

java hibernate jpa spring-data-jpa hibernate-5.x

38
推荐指数
4
解决办法
3万
查看次数

无法自动配置DataSource:未指定"spring.datasource.url"

我已经使用Web,MongoDB和JPA依赖项从SPRING INITIALIZR创建了一个基本的Spring启动应用程序.

当我尝试运行spring boot应用程序时,我收到以下异常:

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2018-03-25 16:27:02.807 ERROR 16256 --- [  restartedMain] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************
Description:
Failed to auto-configure a DataSource: 'spring.datasource.url' is not specified and no embedded datasource could be auto-configured.
Reason: Failed to determine a suitable driver class

Action:

Consider the following situation:
If you want an embedded database like H2, HSQL or Derby, please add it in …
Run Code Online (Sandbox Code Playgroud)

java spring mongodb spring-data-jpa spring-boot

38
推荐指数
3
解决办法
10万
查看次数

org.hibernate.AnnotationException:没有为实体指定标识符 - 即使它是

我有以下配置:

<bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="jpaDataSource" />
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
        </property>
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>
        <property name="packagesToScan">
        <list>
            <value>com.example.domain</value>
            <value>com.example.repositories</value>
        </list>
    </property>
</bean>
Run Code Online (Sandbox Code Playgroud)

我在com.example.domain中有我的Geoname类:

@Entity
@Table(name="geonames")
public class Geoname implements Serializable {

    @Id
    @Column(name="geonameid")
    private Long geonameid = null;
}
Run Code Online (Sandbox Code Playgroud)

但是,在运行时,我得到以下异常:

org.hibernate.AnnotationException:通过引起指定实体没有标识符:com.example.domain.Geoname在org.hibernate.cfg.InheritanceState.determineDefaultAccessType(InheritanceState.java:277)在org.hibernate.cfg.InheritanceState.getElementsToProcess( InheritanceState.java:224)在org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:664)在org.hibernate.cfg.Configuration $ MetadataSourceQueue.processAnnotatedClassesQueue(Configuration.java:3449)在org.hibernate.cfg.Configuration $ MetadataSourceQueue.processMetadata(Configuration.java:3403)在org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1330)在org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1730)

有什么想法吗?

旁注:我在这个项目中将mongodb和hibernate/mysql结合起来.

spring hibernate spring-data spring-data-jpa

37
推荐指数
2
解决办法
6万
查看次数

在JSON中禁用超文本应用程序语言(HAL)?

在版本2.0.2.RELEASE中使用带有JPA的Spring Data REST.

如何在JSON中禁用超文本应用程序语言(HAL)?http://stateless.co/hal_specification.html

我已经尝试了很多东西,但无济于事.例如,我已将Accept和Content-type标头设置为"application/json"而不是"application/hal + json",但我仍然收到带有超链接的JSON内容.

例如,我想得到类似的东西:

{
"name" : "Foo",
"street" : "street Bar",
"streetNumber" : 2,
"streetLetter" : "b",
"postCode" : "D-1253",
"town" : "Munchen",
"country" : "Germany",
"phone" : "+34 4410122000",
"vat" : "000000001",
"employees" : 225,
"sector" : {
     "description" : "Marketing",
     "average profit": 545656665,
     "average employees": 75,
     "average profit per employee": 4556
     }
}
Run Code Online (Sandbox Code Playgroud)

代替:

{
"name" : "Foo",
"street" : "street Bar",
"streetNumber" : 2,
"streetLetter" : "b",
"postCode" : "D-1253",
"town" …
Run Code Online (Sandbox Code Playgroud)

rest json spring-data spring-data-jpa spring-data-rest

37
推荐指数
1
解决办法
2万
查看次数

Spring Data JPA:按示例查询?

使用Spring Data JPA可以通过示例进行查询,其中特定实体实例用作搜索条件吗?

例如(没有双关语),如果我有一个Person看起来像这样的实体:

@Entity
public class Person {
  private String firstName;
  private String lastName;
  private boolean employed;
  private LocalDate dob;
  ...
}
Run Code Online (Sandbox Code Playgroud)

我可以找到所有在1977年1月1日出生的史密斯姓氏的雇员,举个例子:

Person example = new Person();
example.setEmployed(true);
example.setLastName("Smith");
example.setDob(LocalDate.of(1977, Month.JANUARY, 1));
List<Person> foundPersons = personRepository.findByExample(example);
Run Code Online (Sandbox Code Playgroud)

java spring spring-data spring-data-jpa

36
推荐指数
3
解决办法
5万
查看次数