Spring boot property injection to JPA entity definitions - spring

Is it possible to use spring boot property placeholder notations in JPA entity defitinions ?
For instance following is not working :
#Entity
#Table(schema=#Value("schemaname") , name="tablename")
public class InterfaceModel {
Is there a way to make this work ?

No there isn't. Check #829 in the Spring Boot issue tracker for more details.

Related

Spring boot Jpa is not working with Spring batch and spring integration

I am working with spring batch. I needed to add some jpa repositories. So previously i was using JDBCTemplate which was working fine.
But when I started working with JPA, the spring boot application could not find the repos. Which were there.
#Autowired
ClassLevelConfigRepo clcr;
I checked these things as the best practices.
Added #EnableJpaRepositories in springBoot application class.
Added #Repostiories to the repository interfaces.
extended the interfaces with JpaRepository<Account, String>
Added #Entity to the entity classes and defined the #Table and # Column annotations properly.
But I am still getting below error.
Field clcr in com.cloudtask.batchconfig.util.LhmUtility required a bean of type 'com.cloudtask.batchconfig.repo.ClassLevelConfigRepo' that could not be found.
I tried checking all the dependencies in pom.xml it was as per recommended. And I have all the tables defined properly in data base.
I was expecting the application to return the Autowired clcr object propely.
Edit 1 : spring boot application annotations
#SpringBootApplication
#ComponentScan({"com.cloudtask"})
#EnableAsync
#IntegrationComponentScan({"com.cloudtask"})
#EnableIntegrationManagement(defaultLoggingEnabled = "true")
#EnableJpaRepositories
#EntityScan
public class imclassApplication ```
When you work with Spring Data Jpa with those basic points you should also keep track of below points.
you have added spring-boot-starter-data-jpa in your pom.xml
you have added the entity and repo package below one level of the application package.
If you the package is at same level you should specify the exact package details in the annotation. in your case it should be like :
#EnableJpaRepositories("com.cloudtask.batchconfig.repo")
#EntityScan(basePackages = {"com.cloudtask.batchconfig.entity"})
Happy programming!

JPA ddl-auto=create/update with schema= mentioned in Entity not working with spring boot 2.1

I have a spring boot application, with following User entity class:
#Entity
#Table(name="user")
public class User {
...
and I'm using ddl-auto=update (or create) to auto-generate the schema in the database. The database used is H2 (also tried with HSQL).
Now all works well (the required table is automatically generated when application is launched), until the entity class is changed to the following (added schema=):
#Entity
#Table(name="user", schema="myschm")
public class User {
...
Now it gives the error when creating the table: Schema "MYSCHM" not found. It seems JPA is expecting the schema MYSCHM to be present and not creating it automatically.
I started observing this issue after using Spring Boot 2.1.5. This used to work when I was using Spring Boot 1.5.3.
Is there any change made in Spring Boot 2+ that affects this? Is there any configuration change I need to do to make this work?
Thanks
The schema is not auto created by H2.
You have to add:
jdbc:h2:mem:test;INIT=CREATE SCHEMA IF NOT EXISTS MYSCHM

Should we use #Component on #Entity class in Spring boot

Well i am a newbie in spring boot. I was working on a project where I needed to #Autowired my Entity in a controller class. But I ended up with error:
Field repository in abc required a bean of type 'xyz' that could not be found.
But it solved after adding #Component in Entity class.
So my questions are:
Why Spring boot was not scanning my Entity class as it was under #SpringBootApplication declaration?
When and where we should use #Component annotation in our application?
Use #Component to flag your Pojo as Spring Bean, so that you inject it into other beans with #Autowired
Use #Entity to flag your Pojo as JPA or Spring Data managed bean to read or write it to a database

Spring boot DI not working for scala typeclass

Spring boot DI not working for scala typeclass. Throwing NoSuchBeanException. I am using spring 4.3.13 and spring boot version 1.5.9
This is pseudo code snippet for reference:
trait Repo[T]
#Repository
class RedisRepo #Autowired()(redis: RedisClient) extends Repo[Event]
class RedisDao #Autowired()(repo: Repo[Event])
I have also tried using #Qualifier for the constructor parameter in RedisDao class. However, its also not working.
Thanks.

When to use Spring #Autowire annotation

Recently I had discussion with my friend regarding usage of Spring #Autowire annotation on entity(JPA) classes.
In our project we are using #Autowire annotaion to inject Entity but my friend suggesting not to use #Autowire annotaions on entity classes. When I asked why? He dont have the proper answer for that. So i just wanted to know are there any disadvantages using #Autowire annotaion on entity classes.
Also please explain when to go for #Autowire annotaion or not with example.
Thank in advance.
#Entity and #Autowire are not interchangeable.
#Entity annotation indicates that the JavaBean is a persistent entity.This is actually a JPA annotation and not a Spring Annotation.
#Entity will be used in the sessionFactory by the packagesToScan poroerty.
#Autowired: inject a resource by-type, i.e. by the class or by the interface of the annotated field or contractor. See my answer Inject and Resource and Autowired annotations
#Autowired is used to inject dependencies as an alternative to setting it via xml configurations
Maybe this answer will help you understand
Hibernate - spring annotated entities not scanned from within jar
UPDATE:
Following the comment bellow:
Company is your domain object, so you don't need to use spring in this case.
<bean id="company" class="xxx.Company"/>
The above will return the same instance with #autowire.
Even if you switch to scope="prototype" I don't see any reason to use spring for that.
You should have a service that will be used to CRUD company e.g.
CompanyService, this service will be a single tone so you will use #Autowire to inject it to the controller and it will use your JPA framework to implement CRUD's
To create a new company you will use:
Company c = new Company //this probably will be binded from your ui form
companyServic.saveOrUpdate(c);
See the following answer spring rest service - hibernate dao - annotations - pojo - namedqueries.
For common practice of DAO and services.
#Autowire is an annotation used to perform a dependency injection, its almost similar to the standard #Inject you can take a look at the spring reference manual to see the difference between those two annotations.
#Entity is a part of the jpa framework its used to mark a class as persistent, spring does not implement an equivalent annotation.

Resources