如何从ElementCollection中提取数据

时间:2018-12-06 13:09:26

标签: database spring spring-data-jpa

我正在尝试从StandartFont中包含的ElementCollection中提取数据。

public class DBFonts {

@Id
@GeneratedValue(strategy= GenerationType.AUTO)
private long id;
private String nameFont;


@ElementCollection
@CollectionTable(
        name="StandartFont",
        joinColumns=@JoinColumn(name="Fonts_id")
@Lob @Basic(fetch = FetchType.LAZY)
@Column(length=100000)
private List<byte[]> StandartFonts; }

存储库:

public interface FontRepo extends JpaRepository<DBFonts,Long> {
   List<byte[]> findByStandartFonts(Long fonsId);
}

 HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
Hibernate: alter table font add constraint FKpcplr5ixrmh5lbjx0e6peoqo4 foreign key (user_id) references usr (id)
Hibernate: alter table standart_font add constraint FKq7nxy6see56tp2y997fwmsewq foreign key (fonts_id) references font (id)
2018-12-06 16:15:54.338  INFO 6168 --- [  restartedMain] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2018-12-06 16:15:54.884  WARN 6168 --- [  restartedMain] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'addFont': Unsatisfied dependency expressed through field 'fontRepo'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'fontRepo': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract java.util.List spring_mvc.entity.FontRepo.findByStandartFonts(java.lang.Long)! Unable to locate Attribute  with the the given name [standartFonts] on this ManagedType [spring_mvc.entity.DBFonts]
2018-12-06 16:15:54.884  INFO 6168 --- [  restartedMain] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
2018-12-06 16:15:54.886  INFO 6168 --- [  restartedMain] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown initiated...
2018-12-06 16:15:54.896  INFO 6168 --- [  restartedMain] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown completed.
2018-12-06 16:15:54.898  INFO 6168 --- [  restartedMain] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2018-12-06 16:15:54.916  INFO 6168 --- [  restartedMain] ConditionEvaluationReportLoggingListener : 

    Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
    2018-12-06 16:15:54.928 ERROR 6168 --- [  restartedMain] o.s.boot.SpringApplication               : Application run failed

    org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'addFont': Unsatisfied dependency expressed through field 'fontRepo'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'fontRepo': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract java.util.List spring_mvc.entity.FontRepo.findByStandartFonts(java.lang.Long)! Unable to locate Attribute  with the the given name [standartFonts] on this ManagedType [spring_mvc.entity.DBFonts]
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:586) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]

at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) [spring-boot-devtools-2.0.5.RELEASE.jar:2.0.5.RELEASE]
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'fontRepo': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract java.util.List spring_mvc.entity.FontRepo.findByStandartFonts(java.lang.Long)! Unable to locate Attribute  with the the given name [standartFonts] on this ManagedType [spring_mvc.entity.DBFonts]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1699) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]

它不起作用,如何正确提取数据?

会起作用吗?: 从standart_font中选择standart_fonts其中fonts_id =? 如何在@Query中编写

1 个答案:

答案 0 :(得分:1)

Spring在类standartFonts中找不到名称为DBFonts的属性,这就是为什么它无法与您的repository一起创建的原因

  

无法找到具有给定名称[standartFonts]的属性   此ManagedType [spring_mvc.entity.DBFonts]

Spring使用反射从您的存储库中获取方法名称(findByStandartFonts)并将其解析:findBy的意思是selectStandartFonts解析为standartFonts并它会尝试在您的@Entity

中找到此字段

PS。请在id字段中使用Long而不是long。这是一个好习惯。 并在Java代码中使用camelCase ...