【问题标题】:Spring Boot - mapping Entities from external jarSpring Boot - 从外部 jar 映射实体
【发布时间】:2017-08-21 05:47:45
【问题描述】:

我在使用 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 有这个:

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

}

我的错误是什么?

【问题讨论】:

  • 在此处查看最受欢迎的答案:stackoverflow.com/questions/9504824/… 看起来您必须在 persistence.xml 中指定 jar 文件名
  • 是的,但是 Spring Boot 的重点是没有 xml-config。我原以为@EntityScan 就足够了。
  • @benbjo 你解决了这个问题吗?我正在设置一个 Spring Boot 应用程序,但我无法从外部 JAR 加载实体

标签: java spring spring-boot jar spring-data


【解决方案1】:

要扫描 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;
}

【讨论】:

    【解决方案2】:

    我通过使用以下 bean 设置包扫描来完成这项工作:

    @Bean
    public EntityManagerFactory entityManagerFactory() {
        HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        vendorAdapter.setGenerateDdl(false);
        vendorAdapter.setShowSql(false);
        vendorAdapter.setDatabase(Database.MYSQL);
    
        LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
        factory.setJpaVendorAdapter(vendorAdapter);
        factory.setPackagesToScan("add packages here");
        return factory.getObject();
    }
    

    【讨论】:

      猜你喜欢
      • 2020-05-07
      • 2021-10-16
      • 1970-01-01
      • 1970-01-01
      • 2021-09-15
      • 2019-06-12
      • 2021-09-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多