jpa Hibernate make schema name configurable in Entity class - spring

I was developing a simple Spring Boot application in which, jpa + Hibernate is user for accessing my data source, which is Oracle DB. The entity class is given below.
#Entity
#Table(name="MY_SCHEMA.MY_DB")
public class Member implements Serializable {
.............
}
Currently my project doesn't have any persistence.xml. The problem is, I need to make the schema name (MY_SCHEMA) inside #Table annotation configurable, that is getting the schema value from application.properties file on run time.
I have tried by adding spring.jpa.properties.hibernate.default_schema=schema option in application.properties file. But all in vain.
Update
Have added more details in another question Hibernate how to make schema name configurable for entity class

Below are the available options that can be used for your purpose of creating table in a particular Schema. mention the schema name in the schema field.
#Entity
#Table(name = "MY_TABLE_NAME", schema= "MY_SCHEMA_NAME")
public class Myclass {
Also you can define the schema name in the DB URL as below using the application.properties file. you need to update the values as per your needs.
spring.datasource.url=jdbc:mysql://localhost:3306/MY_SCHEMA_NAME?autoReconnect=true&useSSL=false

Related

Is using #Entity annotation in Spring boot JPA creates a table?

BACKGROUND
I am new to developing API in spring boot. I have this project wherein it is connected to an Oracle DB and PostgreSQL. The Oracle DB already have an existing tables and I need to fetch some data from multiple tables and send it back as a response. The Postgres DB is where I store the users data and other some data that doesn't need to be stored in the Oracle DB. I am currently using native queries.
The Account is an entity wherein I just marked one of the columns as the #Id (It is actually not an Id but it is unique for all accounts):
#Entity
#Data
#AllArgsConstructor
#NoArgsConstructor
#Builder
public class Account {
#Id
private String sampleProperty1;
private String sampleProperty2;
private String sampleProperty3;
private String sampleProperty4;
private String sampleProperty5;
}
Now I have a repository interface:
public interface IAccountRepository extends JpaRepository<Account, String> {
#Query(value = "SELECT * FROM TABLE(SAMPLE_PACKAGE.SAMPLE_FUNC_GETACCOUNTS(?1))", nativeQuery = true)
List<Account> getAllAccountsByClientNumber(String clientNumber);
}
I was able to fetch the data and JPA mapped the columns automatically to my entity. Basically I am creating an Entity (Spring boot) for the data in my Oracle DB where the only purpose of it is to map the data and send it back to the user.
QUESTIONS
Will this approach create a table in my Oracle DB? I checked the Oracle DB and there is no table. But I'm worried it might somehow create a table of ACCOUNT in the oracle DB when it is on production. If this might happen, how can I prevent it?
This scenario also applies to other functionality like fetching transaction history, creating transaction, updating the Account data that are all in the Oracle DB. Am I doing it just right or there is a better option?
Is creating an Entity without a corresponding table have a drawback in Spring boot?
Note
I know you might say that I should just use the Oracle DB and create entities based on the existing tables. But in the future of the API, it will not have a connection with the Oracle DB. I already tried using projections it was also good, but I still needed to create a Response model and mapped it then send it back to user and creating a unit tests using the projection is pretty long and it sucks haha
You can set the following property:
spring.jpa.hibernate.ddl-auto=update
update will update your database if database tables are already created and will create if database tables are not created.

spring boot with multiple database - both of them have the same schema name and table name

I have two databases but both of them contain the same table name and schema name.
so when I am creating the entity class how can I specify which class should connect to the first database and the other one to the second database?
Spring Boot does it for you. All you have to do is to show Spring that you need 2 datasources.
For this, you have to do:
Create 2 separate configuration classes.
In each, you have to declare beans: dataSource, entityManager, and transactionalManager.
Mark datasource beans with #ConfigurationProperties annotation with "prefix" which separates datasource props in your properties/yml file.
Configuration class has to be marked with #EnableJpaRepositories annotation. In the parameters of the annotation you have to specify "basePackages" - the value should lead to your entity model (ex: "com.myprj.dao.entity.user"),
"entityManagerFactoryRef" as the name of your entityManager bean,
"transactionManagerRef" as the name of your transactionalManager bean.
Spring will manage the rest of the work.

Is it necessary to use Entity annotation for Select query records from Database

I have spring boot application with JPA and MySQL. The table is already created and data are present. From the spring boot application, we need to create two API - get a particular record using id and get all the records. I created a model class but confused with usage of #Entity. Because I'm not going to insert/delete a record from the database. I want to use only for select the record.
For Select query (findBy), do we need to use #Entity annotation at the top of model class?
Yes, you would need to use #Entity to mark your model class, this tells JPA that your class is used for mapping to and from a database table. If you want to make sure that you don't accidentally overwrite the data, you could implement a check via #EntityListener, as described in this answer.

How to set database schema of an aggregate/entity in spring data jdbc

I am new to spring-data-jdbc and just trying to port a small project, which currently uses JPA, for evaluation purposes.
My existing entities use a database schema which can easily be defined by JPAs #Table annotation on the entity level.
I saw, that a #Table annotation exists for spring-data-jpa, but no schema can be specified.
The only approach I found so far is to override the naming Strategy in the JdbcConfiguration:
#Bean
fun namingStrategy(): NamingStrategy {
return object : NamingStrategy {
override fun getSchema(): String {
return "my_schema"
}
}
}
I would rather prefer an approach, where the schema is specified directly at the entity, to be able to use the same configuration for different schemas.
Are there any other ways to specify the database schema for each aggregate separately?
The answer to my own question is rather trivial:
By using the annotation #Table(value = "my_schema.some_table") on the entity level the proper schema is used.

Create Table Using POJO class spring data cassandr

Create Table from Casssandra POjo class using spring data Cassandra is there any tag to create a table definition
Simply use annotation #Table from org.springframework.data.cassandra.core.mapping.Table and also use #PrimaryKey(value = "id") if your pojo have attribute of id.
For an CassandraConfiguration.class, follow my answer here.

Resources