• Empleos
  • Sobre nosotros
  • profesionales
    • Inicio
    • Empleos
    • Cursos y retos
  • empresas
    • Inicio
    • Publicar vacante
    • Nuestro proceso
    • Precios
    • Evaluaciones
    • Nómina
    • Blog
    • Comercial
    • Calculadora de salario

0

797
Vistas
Spring Boot - mapping Entities from external jar

I am having some trouble with Spring Boot, Spring Data and having Entities in an external jar. Any help would be greatly appreciated!

My Sprint Data repository looks like this:

@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:

@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:

@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. The external jar does not have and @SpringBootApplication anywhere. It is basically just a jar with all my Entities.

My application jar however has this:

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

}

What is my error?

over 3 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

To scan entities residing in jar, you have to set packagesToScan field of LocalSessionFactory.

@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;
}
over 3 years ago · Santiago Trujillo Denunciar

0

I got this working using by using the following bean to set the packages scan:

@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();
}
over 3 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recomiéndame algunas ofertas
Necesito ayuda