簡體   English   中英

Spring Boot - 從外部jar映射實體

[英]Spring Boot - mapping Entities from external jar

我在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;
}

我通過使用以下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();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM