简体   繁体   English

Spring Boot - 从外部jar映射实体

[英]Spring Boot - mapping Entities from external jar

I am having some trouble with Spring Boot, Spring Data and having Entities in an external jar. 我在Spring Boot,Spring Data和外部jar中有实体方面遇到了一些麻烦。 Any help would be greatly appreciated! 任何帮助将不胜感激!

My Sprint Data repository looks like this: 我的Sprint数据存储库如下所示:

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

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

My entity, which is in another jar entirely looks like this: 我的实体,在另一个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;
   }
   [...]

} }

And the BaseEntity looks like this: 而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());
    }

}

So, when I try to run this code I get a long stacktrace which basically ends with: 因此,当我尝试运行此代码时,我得到一个很长的堆栈跟踪,它基本上以:

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

I believe that this may have something to do with the Spring Boot Configuration. 我相信这可能与Spring Boot配置有关。 The external jar does not have and @SpringBootApplication anywhere. 外部jar没有和@SpringBootApplication在任何地方。 It is basically just a jar with all my Entities. 它基本上只是一个包含我所有实体的jar。

My application jar however has this: 我的应用程序jar有这个:

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

}

What is my error? 我的错误是什么?

To scan entities residing in jar, you have to set packagesToScan field of LocalSessionFactory. 要扫描驻留在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;
}

I got this working using by using the following bean to set the packages scan: 我通过使用以下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