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

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.

Related

jpa Hibernate make schema name configurable in Entity class

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

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.

How do I execute named queries from a JPA EntityListener?

I have a requirement to set a date_updated value in my database for each row when that row is updated. Let's call the entity that I'm working with Order, which has a corresponding orders table in the database.
I've added the date_updated column to the orders table. So far, so good.
The #Entity Order object that I'm working with is provided by a third party. I do not have the ability to modify the source code to add a field called dateUpdated. I have no requirement to map this value to the object anyway - the value is going to be used for business intelligence purposes only and does not need to be represented in the Java entity object.
My problem is this: I want to update the date_updated column in the database to the current time each time an Order object (and its corresponding database table row) is modified.
Constraints:
We are using Oracle, Spring, JPA and Hibernate
I cannot use Oracle triggers to update the value. We are using a database replication technology that prevents us from using triggers.
My approach thus far has been to use a JPA EntityListener, defined in xml, similar to this:
<entity-mappings xmlns="....">
<entity class="com.theirs.OrderImpl">
<entity-listeners>
<entity-listener class="com.mine.listener.OrderJPAListener" />
</entity-listeners>
</entity>
</entity-mappings>
My listener class looks like this:
public class OrderJPAListener {
#PostPersist
#PostUpdate
public void recordDateUpdated(Order order) {
// do the update here
}
}
The problem I'm having is injecting any sort of persistence support (or anything at all, really) into my listener. Because JPA loads the listener via its methods, I do not have access to any Spring beans in my listener class.
How do I go about injecting an EntityManager (or any Spring bean) into my listener class so that I can execute a named query to update the date_updated field?
How do I go about injecting an EntityManager (or any Spring bean) into
my listener class so that I can execute a named query to update the
date_updated field?
As noted above JPA 2.1 supports injecting managed beans to an Entity Listener via CDI. Whether or not Spring supports this I am not sure. The folloiwng post proposes a Spring specific solution.
https://guylabs.ch/2014/02/22/autowiring-pring-beans-in-hibernate-jpa-entity-listeners/
A possible alternative approach would be however to override the SQL generated by Hibernate on an update which is possible as detailed below.
https://docs.jboss.org/hibernate/orm/3.6/reference/en-US/html/querysql.html#querysql-cud
This would be straightforward if you had the source as you would just need to add the #SQLUpdate annotation and tag on the additional date_update column. As you don't however you would need to look at redefining the metadata for that Entity via an xml configuration file and defining the sql-update statement as outlined above:
https://docs.jboss.org/hibernate/stable/annotations/reference/en/html/xml-overriding.html#xml-overriding-principles-entity
Since JPA 2.1 Entity Listeners are CDI managed. Have you tried using #PersistenceUnit annotation? Are you using JTA transaction type?
Otherwise you could use Persistence.createEntityManagerFactory within the Listener class to retrieve the Persistence Context.

How can I get the fieldnames of the PK (#Id) of a JPA #Entity

I'm using JPA2 (Hibernate 4) and have a bunch of #Entity classes with my PKs defined using #Id. How can I programatically retrieve the fieldnames of the PKs of each entity class?
The business case for this is that I want to use Spring's BeanUtils.copyProperties method to copy over entity objects, but want to make sure that I ignore the id fields of my entities while copying.
Is there a way to find this information? Does JPA provide this information? I realize that I can use reflection to go through every class, but hoped there would be an easier solution. I looked into scanning the PropertyDescriptors of the class, but cannot seem to find any method on the PropertyDescriptor that would give me the annotations of a property.
Try this:
#PersistenceUnit
private EntityManagerFactory emf;
Metamodel metamodel = emf.getMetamodel();
metamodel.entity(MyEntity.class).getId();

Can i write two separate pojo classes one for validation and another for hibernate mapping

If yes, how to map them. I am using jsr bean validation and hibernate mapping annotations.
You don't have to do this by mapping two classes to one table.
You can just define one class A which map to a table as a PO (Persistent Object), and define another class B which transfer values amgonst View, Controler and Service Layer as a DTO/VO (Data Transfer Object/Value Object) for validation or data transfering. use BeanUtils to copy properties between PO and DTO.

Resources