java - Spring Boot - 从外部 jar 映射实体

标签 java spring spring-boot jar spring-data

我在 Spring Boot、Spring Data 和外部 jar 中的实体方面遇到了一些问题。任何帮助将不胜感激!

我的 Sprint 数据存储库如下所示:

@Repository
public interface MyFileRepository extends PagingAndSortingRepository<MyFile, Long> {

   @Modifying
   @Transactional
   @Query("Delete from MyFile f where f.created < ?1")
   long deleteOldEntities(Date cutoffDate);
}

我的实体位于另一个 jar 中,完全如下所示:

@Entity
@SequenceGenerator(
   name = "SequenceIdGenerator",
   sequenceName = "SEQ_ID_MY_FILE",
   allocationSize = 20
)
@Table(
   name = "MYFILE_TABLE"
)
public class MyFile extends BaseEntity {

   private long id;  
   private byte[] data;
   [...]

   public MyFile() {}

   @Id
   @Column(
      name = "id",
      nullable = false
   )
   @GeneratedValue(
      generator = "SequenceIdGenerator"
   )
   public long getId() {
      return this.id;
   }

   public void setId(long id) {
      this.id = id;
   }
   [...]

}

BaseEntity 看起来像这样:

@MappedSuperclass
public abstract class BaseEntity implements Serializable {
    private static final long serialVersionUID = 1L;
    private static final Charset UTF_8 = Charset.forName("UTF-8");
    private Date created = null;
    private Date updated = null;

    public BaseEntity() {}

    @Column(
       name = "created"
    )
    @Temporal(TemporalType.TIMESTAMP)
    public Date getCreated() {
        return this.created == null?null:new Date(this.created.getTime());
    }

    public void setCreated(Date created) {
    if(created != null) {
        this.created = new Date(created.getTime());
    }

}

因此,当我尝试运行此代码时,我得到一个很长的堆栈跟踪,其基本上以以下内容结尾:

Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: MyFile is not mapped [Delete from MyFile f where f.created < ?1]

我认为这可能与Spring Boot配置有关。外部 jar 在任何地方都没有 @SpringBootApplication 。它基本上只是一个包含我所有实体的 jar 。

我的应用程序 jar 有这个:

@SpringBootApplication
@EntityScan("myapp.service.dao.entity") --> This is the package where all my entities are located. 
public class CommonApplication {

}

我的错误是什么?

最佳答案

要扫描驻留在 jar 中的实体,您必须设置 LocalSessionFactory 的packagesToScan 字段。

@Bean
public LocalSessionFactoryBean sessionFactory(DataSource dataSource) {
    LocalSessionFactoryBean localSessionFactory = new LocalSessionFactoryBean();
    localSessionFactory.setDataSource(dataSource);
    localSessionFactory
            .setPackagesToScan(new String[]{"myapp.service.dao.entity", "com.application.entity"});
    return localSessionFactory;
}

关于java - Spring Boot - 从外部 jar 映射实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43064717/

相关文章:

spring - 将带有 Spring RestTemplate 的 byte[] 上传到 SpringMVC rest 端点时出现 400 Bad Request

spring-boot - Hibernate Spatial PostGis PSQLException 列的类型为 point 但表达式的类型为 bytea

java - 如何在 jax-ws 客户端中隐藏警告(可能)由 jax-ws 库引起

JavaMail - 设置端口、代理和防火墙

java - 有没有办法用任务管理器找到Java中运行的线程数?

java - 使用 H2 的永久数据库

java - Spring Boot 无法设置缓存

java - 将 JButton 放在右下角

Java REST Spring 3.2.3 apache cxf 2.7.5 生成 XML 正常,但无法生成 JSON

java - 数组和对象的 Jackson 反序列化问题