I am working on a spring boot application,
application is working fine, reading from the DB with no issue.
//working fine on schema PRINTERS_SCHEMA
public interface PrinterRepository extends CrudRepository<PrinterInfo,String>{}
my question is how to go about adding a new Repository reading from a different oracle schema?
//need this repository to read from EMPLOYEE_ SCHEMA
public interface EmployeeRepository extends CrudRepository<EmployeeInfo,String>{}
As you are using 2 different entities, you can specify the schema on the entity class:
#Entity
#Table(name = "employee", schema = "employeeschema")
public class EmployeeInfo
Related
Can you use both annotations on your database tables?
id, just like some clarification on there differences. thank you
#Entity is used to map a class to a relational database, it represents a database table.
#Document is used to map a class to noSQL database (specifically mongoDB), it represents a MongoDB documents.
You can use both JPA or MongoRepository if you are using both databases by creating different entities and repositories for each database.
I recommend you to have a look at spring documentation (https://docs.spring.io/spring-data/jpa/docs/current/reference/html/)
#Document is a Spring Data Mongo annotation while #Entity is part of Java Persistence API (JPA).
You can check both documentations:
Spring Data Mongo docs
JPA docs
Where into "Example 10. Repository definitions using domain classes with annotations" there is this piece of code:
interface PersonRepository extends Repository<Person, Long> { … }
#Entity
class Person { … }
interface UserRepository extends Repository<User, Long> { … }
#Document
class User { … }
And documentation says:
PersonRepository references Person, which is annotated with the JPA #Entity annotation, so this repository clearly belongs to Spring Data JPA. UserRepository references User, which is annotated with Spring Data MongoDB’s #Document annotation.
So you can see here the difference.
I have a legacy spring mvc application which is using spring jdbc as ORM. Now I wanted to use spring boot and instead of mvc I will be converting it in to RestAPI. I have problem with database query part. Using propertyplaceholder xml confi the external sql query properties file is configured in the old application. Using Spring boot and latest annotation methods how can I configure this. My understanding is
Place the query properties file in src/main/resources directory
In DAO create property name same as the key ( name ) of the sql.
create getter and setter for the key property
Is this is the right approach ? if yes if I use this how I will get the query in my DAO class ?. If not what is the best method.
It is not a good approach but you can do it in several ways and I am always approaching to use JPA when you work with database and spring boot
application.yml
emp:
eid: "select eid from employee"
name: "select ename from employee"
create a class for Employee under the model package
#ConfigurationProperties("emp")
#Getter
#Setter
public class Employee
{
private List<Integer> eid;
private List<String> ename;
}
create a bean instance for Employee under the config package
#Configuration
public class Config
{
#Bean
public Employee getEmployee()
{
return new Employee();
}
}
then you can call Employee instance whenever you need in your project
#RestController
public class EmpController
{
#Autowired
Employee emp;
}
After posting this question I didn't get much response. So I did some work from my side and the fixed it same way as I mentioned in the question. I didn't find any other method.
First a java class ApplicationPropertyConfig for PropertySourcesPlaceholderConfigurer . Property source file is my sql properties file.
#Configuration
#PropertySource("classpath:appSql.properties")
public class ApplicationPropertyConfig {
#Bean
public static PropertySourcesPlaceholderConfigurer placeHolderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
}
appSql.properties
selectallemployees=select * from employee;
created another class with name ApplicationSQLWrapper.java which is annotated with #Component. This contains the property name same as the sql properties key with #value annotation.
#Component
public class ApplicationSQLWrapper{
#Value("${selectallemployees}")
private String selectallemployees;
//getter and setter
This SQL's can be accessed from DAO class by creating an Object of the Component class.
I am working on Spring Boot application.
We have Service Layer,Rest Controller and Dao as repository.
I have 20 to 30 tables in my database and I dont want to create repository for each entity and extends that to CrudRepository.
ex : User is an Entity, to perform persistance operations on User, I have to create UserRepository which extends CrudRepository.
Same with Department, Company etc...
What i want to do is, I will write a BaseRepository which gonna extend CrudRepository, base repository should accept all entities and do persistance operations.
Is there a way to that ??
Don't extend CrudRepository it's functionality is all tied to the generic type, it'd be hacky to extend it for a generic implementation. You probably just want something simple which uses the JPA entity manager directly:
import javax.persistence.EntityManager;
import org.springframework.beans.factory.annotation.Autowired;
public class GenericRepository {
#Autowired
private EntityManager entityManager;
public <T, ID> T findById(Class<T> type, ID id) {
return entityManager.find(type, id);
}
}
I'm using Spring Data MongoDB repository abstraction to access mongoDB database:
public interface CustomerRepository extends MongoRepository<Customer, String> {}
...
#Autowired
private CustomerRepository customerRepository;
List<Customer> customers = customerRepository.findAll();
this will fetch all customers from mongoDB collection.
I'd like to add criteria for all CustomerRepository methods to filter customers by{"active": true} (the actual filter is more complicated if it's matter) transparently (without changing interface).
I'm thinking about overriding some base Spring Data MongoDB classes but I'm not sure how to do it.
Thanks.
This is a follow-up question based on Oliver Gierke's suggestion.
We have two tables (almost same information) but for some external reasons, cannot use a common single table. I am getting an error that the base class is not a mapped entity. Oliver Gierke has mentioned in his response that it would work only for Single Table. I am assuming that is the reason. if so, could someone explain why such limitation and how can I make the following work.
Base entity:
#MappedSuperclass
public abstract class DecisionEntity {
Inherited classes:
#Entity
#Table(name="DM_INSP_TASKING_RULES_RSLT")
public class DmInspTaskingRulesRslt extends DecisionEntity implements Serializable {
#Entity
#Table(name="DM_UW_REF_RULES_RSLT")
public class DmUwRefRulesRslt extends DecisionEntity implements Serializable {
The Repository
#Repository
public interface DecisionManagementRepository<T extends DecisionEntity> extends JpaRepository<DecisionEntity, Long> {
Have defined 'packagesToScan' and also listed all the 3 classes in persistence.xml.
I am getting the 'Non an Managed Entity' for 'DecisionEntity' class.
I tried Inheritence Type - 'TABLE_PER_CLASS
This is not supported by Spring Data JPA and Java Persistance API Specification.
Spring Data JPA Issue DATAJPA-264
Repositories: throw exception at startup if entity is a not an #Entity (e.g. for #MappedSuperclass)
Status: Investigating
Resolution: Unresolved
The JPA specifications says:
A mapped superclass, unlike an entity, is not queryable and must not be passed as an argument to
EntityManager or Query operations. Persistent relationships defined by a mapped superclass must
be unidirectional.