Merge branch 'master' into task/JAVA-26765
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
## Atomikos
|
||||
This module contains articles about Atomikos
|
||||
|
||||
### Relevant Articles:
|
||||
|
||||
- [A Guide to Atomikos](https://www.baeldung.com/java-atomikos)
|
||||
@@ -0,0 +1,104 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>atomikos</artifactId>
|
||||
<name>atomikos</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>persistence-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.atomikos</groupId>
|
||||
<artifactId>transactions-jdbc</artifactId>
|
||||
<version>${atomikos-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.atomikos</groupId>
|
||||
<artifactId>transactions-jms</artifactId>
|
||||
<version>${atomikos-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.atomikos</groupId>
|
||||
<artifactId>transactions-hibernate4</artifactId>
|
||||
<version>${atomikos-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context</artifactId>
|
||||
<version>${spring-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-tx</artifactId>
|
||||
<version>${spring-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.data</groupId>
|
||||
<artifactId>spring-data-jpa</artifactId>
|
||||
<version>${spring-data-jpa.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-test</artifactId>
|
||||
<version>${spring-version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hibernate</groupId>
|
||||
<artifactId>hibernate-core</artifactId>
|
||||
<version>${hibernate.version}</version>
|
||||
<scope>provided</scope>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>javax.transaction</groupId>
|
||||
<artifactId>jta</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.activemq</groupId>
|
||||
<artifactId>activemq-core</artifactId>
|
||||
<version>${activemq-core.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.derby</groupId>
|
||||
<artifactId>derby</artifactId>
|
||||
<version>${derby.version}</version>
|
||||
</dependency>
|
||||
<!-- the JTA API -->
|
||||
<dependency>
|
||||
<groupId>javax.transaction</groupId>
|
||||
<artifactId>jta</artifactId>
|
||||
<version>${jta.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.geronimo.specs</groupId>
|
||||
<artifactId>geronimo-jta_1.0.1B_spec</artifactId>
|
||||
<version>${geronimo.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hibernate.validator</groupId>
|
||||
<artifactId>hibernate-validator</artifactId>
|
||||
<version>${hibernate-validator.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<atomikos-version>5.0.6</atomikos-version>
|
||||
<spring-version>5.1.6.RELEASE</spring-version>
|
||||
<hibernate.version>5.4.3.Final</hibernate.version>
|
||||
<spring-data-jpa.version>1.11.23.RELEASE</spring-data-jpa.version>
|
||||
<activemq-core.version>5.7.0</activemq-core.version>
|
||||
<derby.version>10.8.1.2</derby.version>
|
||||
<jta.version>1.1</jta.version>
|
||||
<geronimo.version>1.0</geronimo.version>
|
||||
<hibernate-validator.version>8.0.1.Final</hibernate-validator.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
+53
@@ -0,0 +1,53 @@
|
||||
package com.baeldung.atomikos.direct;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.Statement;
|
||||
import java.util.UUID;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import com.atomikos.icatch.jta.UserTransactionImp;
|
||||
|
||||
public class Application {
|
||||
|
||||
private DataSource inventoryDataSource;
|
||||
private DataSource orderDataSource;
|
||||
|
||||
public Application(DataSource inventoryDataSource, DataSource orderDataSource) {
|
||||
this.inventoryDataSource = inventoryDataSource;
|
||||
this.orderDataSource = orderDataSource;
|
||||
}
|
||||
|
||||
public void placeOrder(String productId, int amount) throws Exception {
|
||||
|
||||
UserTransactionImp utx = new UserTransactionImp();
|
||||
String orderId = UUID.randomUUID()
|
||||
.toString();
|
||||
boolean rollback = false;
|
||||
try {
|
||||
utx.begin();
|
||||
Connection inventoryConnection = inventoryDataSource.getConnection();
|
||||
Connection orderConnection = orderDataSource.getConnection();
|
||||
Statement s1 = inventoryConnection.createStatement();
|
||||
String q1 = "update Inventory set balance = balance - " + amount + " where productId ='" + productId + "'";
|
||||
s1.executeUpdate(q1);
|
||||
s1.close();
|
||||
Statement s2 = orderConnection.createStatement();
|
||||
String q2 = "insert into Orders values ( '" + orderId + "', '" + productId + "', " + amount + " )";
|
||||
s2.executeUpdate(q2);
|
||||
s2.close();
|
||||
inventoryConnection.close();
|
||||
orderConnection.close();
|
||||
} catch (Exception e) {
|
||||
System.out.println(e.getMessage());
|
||||
rollback = true;
|
||||
} finally {
|
||||
if (!rollback)
|
||||
utx.commit();
|
||||
else
|
||||
utx.rollback();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
package com.baeldung.atomikos.spring;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.Statement;
|
||||
import java.util.UUID;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
public class Application {
|
||||
|
||||
private DataSource inventoryDataSource;
|
||||
private DataSource orderDataSource;
|
||||
|
||||
public Application(DataSource inventoryDataSource, DataSource orderDataSource) {
|
||||
this.inventoryDataSource = inventoryDataSource;
|
||||
this.orderDataSource = orderDataSource;
|
||||
}
|
||||
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void placeOrder(String productId, int amount) throws Exception {
|
||||
|
||||
String orderId = UUID.randomUUID()
|
||||
.toString();
|
||||
Connection inventoryConnection = inventoryDataSource.getConnection();
|
||||
Connection orderConnection = orderDataSource.getConnection();
|
||||
Statement s1 = inventoryConnection.createStatement();
|
||||
String q1 = "update Inventory set balance = balance - " + amount + " where productId ='" + productId + "'";
|
||||
s1.executeUpdate(q1);
|
||||
s1.close();
|
||||
Statement s2 = orderConnection.createStatement();
|
||||
String q2 = "insert into Orders values ( '" + orderId + "', '" + productId + "', " + amount + " )";
|
||||
s2.executeUpdate(q2);
|
||||
s2.close();
|
||||
inventoryConnection.close();
|
||||
orderConnection.close();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+68
@@ -0,0 +1,68 @@
|
||||
package com.baeldung.atomikos.spring.config;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
import javax.transaction.SystemException;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||
import org.springframework.transaction.jta.JtaTransactionManager;
|
||||
|
||||
import com.atomikos.icatch.jta.UserTransactionManager;
|
||||
import com.atomikos.jdbc.AtomikosDataSourceBean;
|
||||
import com.baeldung.atomikos.spring.Application;
|
||||
|
||||
@Configuration
|
||||
@EnableTransactionManagement
|
||||
public class Config {
|
||||
|
||||
@Bean(initMethod = "init", destroyMethod = "close")
|
||||
public AtomikosDataSourceBean inventoryDataSource() {
|
||||
AtomikosDataSourceBean dataSource = new AtomikosDataSourceBean();
|
||||
dataSource.setLocalTransactionMode(true);
|
||||
dataSource.setUniqueResourceName("db1");
|
||||
dataSource.setXaDataSourceClassName("org.apache.derby.jdbc.EmbeddedXADataSource");
|
||||
Properties xaProperties = new Properties();
|
||||
xaProperties.put("databaseName", "db1");
|
||||
xaProperties.put("createDatabase", "create");
|
||||
dataSource.setXaProperties(xaProperties);
|
||||
dataSource.setPoolSize(10);
|
||||
return dataSource;
|
||||
}
|
||||
|
||||
@Bean(initMethod = "init", destroyMethod = "close")
|
||||
public AtomikosDataSourceBean orderDataSource() {
|
||||
AtomikosDataSourceBean dataSource = new AtomikosDataSourceBean();
|
||||
dataSource.setLocalTransactionMode(true);
|
||||
dataSource.setUniqueResourceName("db2");
|
||||
dataSource.setXaDataSourceClassName("org.apache.derby.jdbc.EmbeddedXADataSource");
|
||||
Properties xaProperties = new Properties();
|
||||
xaProperties.put("databaseName", "db2");
|
||||
xaProperties.put("createDatabase", "create");
|
||||
dataSource.setXaProperties(xaProperties);
|
||||
dataSource.setPoolSize(10);
|
||||
return dataSource;
|
||||
}
|
||||
|
||||
@Bean(initMethod = "init", destroyMethod = "close")
|
||||
public UserTransactionManager userTransactionManager() throws SystemException {
|
||||
UserTransactionManager userTransactionManager = new UserTransactionManager();
|
||||
userTransactionManager.setTransactionTimeout(300);
|
||||
userTransactionManager.setForceShutdown(true);
|
||||
return userTransactionManager;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public JtaTransactionManager jtaTransactionManager() throws SystemException {
|
||||
JtaTransactionManager jtaTransactionManager = new JtaTransactionManager();
|
||||
jtaTransactionManager.setTransactionManager(userTransactionManager());
|
||||
jtaTransactionManager.setUserTransaction(userTransactionManager());
|
||||
return jtaTransactionManager;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Application application() {
|
||||
return new Application(inventoryDataSource(), orderDataSource());
|
||||
}
|
||||
}
|
||||
+48
@@ -0,0 +1,48 @@
|
||||
package com.baeldung.atomikos.spring.jpa;
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
|
||||
import jakarta.validation.ConstraintViolation;
|
||||
import jakarta.validation.Validation;
|
||||
import jakarta.validation.Validator;
|
||||
import jakarta.validation.ValidatorFactory;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import com.baeldung.atomikos.spring.jpa.inventory.Inventory;
|
||||
import com.baeldung.atomikos.spring.jpa.inventory.InventoryRepository;
|
||||
import com.baeldung.atomikos.spring.jpa.order.Order;
|
||||
import com.baeldung.atomikos.spring.jpa.order.OrderRepository;
|
||||
|
||||
public class Application {
|
||||
|
||||
@Autowired
|
||||
private InventoryRepository inventoryRepository;
|
||||
|
||||
@Autowired
|
||||
private OrderRepository orderRepository;
|
||||
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void placeOrder(String productId, int amount) throws Exception {
|
||||
|
||||
String orderId = UUID.randomUUID()
|
||||
.toString();
|
||||
Inventory inventory = inventoryRepository.findOne(productId);
|
||||
inventory.setBalance(inventory.getBalance() - amount);
|
||||
inventoryRepository.save(inventory);
|
||||
Order order = new Order();
|
||||
order.setOrderId(orderId);
|
||||
order.setProductId(productId);
|
||||
order.setAmount(new Long(amount));
|
||||
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
|
||||
Validator validator = factory.getValidator();
|
||||
Set<ConstraintViolation<Order>> violations = validator.validate(order);
|
||||
if (violations.size() > 0)
|
||||
throw new Exception("Invalid instance of an order.");
|
||||
orderRepository.save(order);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
package com.baeldung.atomikos.spring.jpa.config;
|
||||
|
||||
import javax.transaction.SystemException;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||
import org.springframework.transaction.jta.JtaTransactionManager;
|
||||
|
||||
import com.atomikos.icatch.jta.UserTransactionManager;
|
||||
import com.baeldung.atomikos.spring.jpa.Application;
|
||||
|
||||
@Configuration
|
||||
@EnableTransactionManagement
|
||||
public class Config {
|
||||
|
||||
@Bean(initMethod = "init", destroyMethod = "close")
|
||||
public UserTransactionManager userTransactionManager() throws SystemException {
|
||||
UserTransactionManager userTransactionManager = new UserTransactionManager();
|
||||
userTransactionManager.setTransactionTimeout(300);
|
||||
userTransactionManager.setForceShutdown(true);
|
||||
return userTransactionManager;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public JtaTransactionManager transactionManager() throws SystemException {
|
||||
JtaTransactionManager jtaTransactionManager = new JtaTransactionManager();
|
||||
jtaTransactionManager.setTransactionManager(userTransactionManager());
|
||||
jtaTransactionManager.setUserTransaction(userTransactionManager());
|
||||
return jtaTransactionManager;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Application application() {
|
||||
return new Application();
|
||||
}
|
||||
|
||||
}
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
package com.baeldung.atomikos.spring.jpa.inventory;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@Table(name = "INVENTORY")
|
||||
public class Inventory {
|
||||
|
||||
@Id
|
||||
private String productId;
|
||||
private Long balance;
|
||||
|
||||
public String getProductId() {
|
||||
return productId;
|
||||
}
|
||||
|
||||
public void setProductId(String productId) {
|
||||
this.productId = productId;
|
||||
}
|
||||
|
||||
public Long getBalance() {
|
||||
return balance;
|
||||
}
|
||||
|
||||
public void setBalance(Long balance) {
|
||||
this.balance = balance;
|
||||
}
|
||||
|
||||
}
|
||||
+53
@@ -0,0 +1,53 @@
|
||||
package com.baeldung.atomikos.spring.jpa.inventory;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
import javax.persistence.EntityManagerFactory;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
||||
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
|
||||
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
|
||||
|
||||
import com.atomikos.jdbc.AtomikosDataSourceBean;
|
||||
|
||||
@Configuration
|
||||
@EnableJpaRepositories(basePackages = "com.baeldung.atomikos.spring.jpa.inventory", entityManagerFactoryRef = "inventoryEntityManager", transactionManagerRef = "transactionManager")
|
||||
public class InventoryConfig {
|
||||
|
||||
@Bean(initMethod = "init", destroyMethod = "close")
|
||||
public AtomikosDataSourceBean inventoryDataSource() {
|
||||
AtomikosDataSourceBean dataSource = new AtomikosDataSourceBean();
|
||||
dataSource.setLocalTransactionMode(true);
|
||||
dataSource.setUniqueResourceName("db1");
|
||||
dataSource.setXaDataSourceClassName("org.apache.derby.jdbc.EmbeddedXADataSource");
|
||||
Properties xaProperties = new Properties();
|
||||
xaProperties.put("databaseName", "db1");
|
||||
xaProperties.put("createDatabase", "create");
|
||||
dataSource.setXaProperties(xaProperties);
|
||||
dataSource.setPoolSize(10);
|
||||
return dataSource;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public EntityManagerFactory inventoryEntityManager() {
|
||||
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
|
||||
LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
|
||||
factory.setJpaVendorAdapter(vendorAdapter);
|
||||
factory.setPackagesToScan("com.baeldung.atomikos.spring.jpa.inventory");
|
||||
factory.setDataSource(inventoryDataSource());
|
||||
Properties jpaProperties = new Properties();
|
||||
//jpaProperties.put("hibernate.show_sql", "true");
|
||||
//jpaProperties.put("hibernate.format_sql", "true");
|
||||
jpaProperties.put("hibernate.dialect", "org.hibernate.dialect.DerbyDialect");
|
||||
jpaProperties.put("hibernate.current_session_context_class", "jta");
|
||||
jpaProperties.put("javax.persistence.transactionType", "jta");
|
||||
jpaProperties.put("hibernate.transaction.manager_lookup_class", "com.atomikos.icatch.jta.hibernate3.TransactionManagerLookup");
|
||||
jpaProperties.put("hibernate.hbm2ddl.auto", "create-drop");
|
||||
factory.setJpaProperties(jpaProperties);
|
||||
factory.afterPropertiesSet();
|
||||
return factory.getObject();
|
||||
}
|
||||
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
package com.baeldung.atomikos.spring.jpa.inventory;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public interface InventoryRepository extends JpaRepository<Inventory, String> {
|
||||
|
||||
}
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
package com.baeldung.atomikos.spring.jpa.order;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Table;
|
||||
import jakarta.validation.constraints.Max;
|
||||
|
||||
@Entity
|
||||
@Table(name = "ORDERS")
|
||||
public class Order {
|
||||
|
||||
@Id
|
||||
private String orderId;
|
||||
private String productId;
|
||||
@Max(5)
|
||||
private Long amount;
|
||||
|
||||
public String getOrderId() {
|
||||
return orderId;
|
||||
}
|
||||
|
||||
public void setOrderId(String orderId) {
|
||||
this.orderId = orderId;
|
||||
}
|
||||
|
||||
public String getProductId() {
|
||||
return productId;
|
||||
}
|
||||
|
||||
public void setProductId(String productId) {
|
||||
this.productId = productId;
|
||||
}
|
||||
|
||||
public Long getAmount() {
|
||||
return amount;
|
||||
}
|
||||
|
||||
public void setAmount(Long amount) {
|
||||
this.amount = amount;
|
||||
}
|
||||
|
||||
}
|
||||
+53
@@ -0,0 +1,53 @@
|
||||
package com.baeldung.atomikos.spring.jpa.order;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
import javax.persistence.EntityManagerFactory;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
||||
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
|
||||
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
|
||||
|
||||
import com.atomikos.jdbc.AtomikosDataSourceBean;
|
||||
|
||||
@Configuration
|
||||
@EnableJpaRepositories(basePackages = "com.baeldung.atomikos.spring.jpa.order", entityManagerFactoryRef = "orderEntityManager", transactionManagerRef = "transactionManager")
|
||||
public class OrderConfig {
|
||||
|
||||
@Bean(initMethod = "init", destroyMethod = "close")
|
||||
public AtomikosDataSourceBean orderDataSource() {
|
||||
AtomikosDataSourceBean dataSource = new AtomikosDataSourceBean();
|
||||
dataSource.setLocalTransactionMode(true);
|
||||
dataSource.setUniqueResourceName("db2");
|
||||
dataSource.setXaDataSourceClassName("org.apache.derby.jdbc.EmbeddedXADataSource");
|
||||
Properties xaProperties = new Properties();
|
||||
xaProperties.put("databaseName", "db2");
|
||||
xaProperties.put("createDatabase", "create");
|
||||
dataSource.setXaProperties(xaProperties);
|
||||
dataSource.setPoolSize(10);
|
||||
return dataSource;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public EntityManagerFactory orderEntityManager() {
|
||||
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
|
||||
LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
|
||||
factory.setJpaVendorAdapter(vendorAdapter);
|
||||
factory.setPackagesToScan("com.baeldung.atomikos.spring.jpa.order");
|
||||
factory.setDataSource(orderDataSource());
|
||||
Properties jpaProperties = new Properties();
|
||||
//jpaProperties.put("hibernate.show_sql", "true");
|
||||
//jpaProperties.put("hibernate.format_sql", "true");
|
||||
jpaProperties.put("hibernate.dialect", "org.hibernate.dialect.DerbyDialect");
|
||||
jpaProperties.put("hibernate.current_session_context_class", "jta");
|
||||
jpaProperties.put("javax.persistence.transactionType", "jta");
|
||||
jpaProperties.put("hibernate.transaction.manager_lookup_class", "com.atomikos.icatch.jta.hibernate3.TransactionManagerLookup");
|
||||
jpaProperties.put("hibernate.hbm2ddl.auto", "create-drop");
|
||||
factory.setJpaProperties(jpaProperties);
|
||||
factory.afterPropertiesSet();
|
||||
return factory.getObject();
|
||||
}
|
||||
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
package com.baeldung.atomikos.spring.jpa.order;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public interface OrderRepository extends JpaRepository<Order, String> {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
||||
@@ -0,0 +1,10 @@
|
||||
CREATE TABLE INVENTORY (
|
||||
productId VARCHAR PRIMARY KEY,
|
||||
balance INT
|
||||
);
|
||||
|
||||
CREATE TABLE ORDERS (
|
||||
orderId VARCHAR PRIMARY KEY,
|
||||
productId VARCHAR,
|
||||
amount INT NOT NULL CHECK (amount <= 5)
|
||||
);
|
||||
@@ -0,0 +1 @@
|
||||
com.atomikos.icatch.file=logs
|
||||
+118
@@ -0,0 +1,118 @@
|
||||
package com.baeldung.atomikos.direct;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.Properties;
|
||||
import java.util.UUID;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.atomikos.icatch.jta.UserTransactionImp;
|
||||
import com.atomikos.jdbc.AtomikosDataSourceBean;
|
||||
|
||||
public class ApplicationUnitTest {
|
||||
|
||||
private static DataSource inventoryDataSource;
|
||||
private static DataSource orderDataSource;
|
||||
|
||||
private static String productId = UUID.randomUUID()
|
||||
.toString();
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void testPlaceOrderSuccess() throws Exception {
|
||||
int amount = 1;
|
||||
long initialBalance = getBalance(inventoryDataSource, productId);
|
||||
Application application = new Application(inventoryDataSource, orderDataSource);
|
||||
application.placeOrder(productId, amount);
|
||||
long finalBalance = getBalance(inventoryDataSource, productId);
|
||||
assertEquals(initialBalance - amount, finalBalance);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void testPlaceOrderFailure() throws Exception {
|
||||
int amount = 10;
|
||||
long initialBalance = getBalance(inventoryDataSource, productId);
|
||||
Application application = new Application(inventoryDataSource, orderDataSource);
|
||||
application.placeOrder(productId, amount);
|
||||
long finalBalance = getBalance(inventoryDataSource, productId);
|
||||
assertEquals(initialBalance, finalBalance);
|
||||
}
|
||||
|
||||
@BeforeClass
|
||||
public static void setUp() throws SQLException {
|
||||
|
||||
inventoryDataSource = getDataSource("db1");
|
||||
orderDataSource = getDataSource("db2");
|
||||
Connection inventoryConnection = inventoryDataSource.getConnection();
|
||||
Connection orderConnection = orderDataSource.getConnection();
|
||||
String createInventoryTable = "create table Inventory ( " + " productId VARCHAR ( 100 ) PRIMARY KEY, balance INT )";
|
||||
String createInventoryRow = "insert into Inventory values ( '" + productId + "', 10000 )";
|
||||
Statement s1 = inventoryConnection.createStatement();
|
||||
try {
|
||||
s1.executeUpdate(createInventoryTable);
|
||||
} catch (Exception e) {
|
||||
System.out.println("Inventory table exists");
|
||||
}
|
||||
try {
|
||||
s1.executeUpdate(createInventoryRow);
|
||||
} catch (Exception e) {
|
||||
System.out.println("Product row exists");
|
||||
}
|
||||
s1.close();
|
||||
String createOrderTable = "create table Orders ( orderId VARCHAR ( 100 ) PRIMARY KEY, productId VARCHAR ( 100 ), amount INT NOT NULL CHECK (amount <= 5) )";
|
||||
Statement s2 = orderConnection.createStatement();
|
||||
try {
|
||||
s2.executeUpdate(createOrderTable);
|
||||
} catch (Exception e) {
|
||||
System.out.println("Orders table exists");
|
||||
}
|
||||
s2.close();
|
||||
inventoryConnection.close();
|
||||
orderConnection.close();
|
||||
}
|
||||
|
||||
private static DataSource getDataSource(String db) {
|
||||
|
||||
DataSource ds;
|
||||
AtomikosDataSourceBean ads = new AtomikosDataSourceBean();
|
||||
ads.setXaDataSourceClassName("org.apache.derby.jdbc.EmbeddedXADataSource");
|
||||
Properties properties = new Properties();
|
||||
properties.put("databaseName", db);
|
||||
properties.put("createDatabase", "create");
|
||||
ads.setXaProperties(properties);
|
||||
ads.setUniqueResourceName(db);
|
||||
ads.setPoolSize(10); // optional
|
||||
ads.setBorrowConnectionTimeout(10); // optional
|
||||
ds = ads;
|
||||
return ds;
|
||||
|
||||
}
|
||||
|
||||
private static long getBalance(DataSource inventoryDataSource, String productId) throws Exception {
|
||||
|
||||
UserTransactionImp utx = new UserTransactionImp();
|
||||
utx.begin();
|
||||
Connection inventoryConnection = inventoryDataSource.getConnection();
|
||||
Statement s1 = inventoryConnection.createStatement();
|
||||
String q1 = "select balance from Inventory where productId='" + productId + "'";
|
||||
ResultSet rs1 = s1.executeQuery(q1);
|
||||
if (rs1 == null || !rs1.next())
|
||||
throw new Exception("Product not found: " + productId);
|
||||
long balance = rs1.getLong(1);
|
||||
inventoryConnection.close();
|
||||
utx.commit();
|
||||
return balance;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+108
@@ -0,0 +1,108 @@
|
||||
package com.baeldung.atomikos.spring;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.UUID;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import com.baeldung.atomikos.spring.config.Config;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = { Config.class })
|
||||
public class ApplicationUnitTest {
|
||||
|
||||
private static String productId = UUID.randomUUID()
|
||||
.toString();
|
||||
|
||||
@Autowired
|
||||
Application application;
|
||||
|
||||
@Autowired
|
||||
DataSource inventoryDataSource;
|
||||
|
||||
@Autowired
|
||||
DataSource orderDataSource;
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void testPlaceOrderSuccess() throws Exception {
|
||||
int amount = 1;
|
||||
long initialBalance = getBalance(inventoryDataSource, productId);
|
||||
application.placeOrder(productId, amount);
|
||||
long finalBalance = getBalance(inventoryDataSource, productId);
|
||||
assertEquals(initialBalance - amount, finalBalance);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void testPlaceOrderFailure() throws Exception {
|
||||
int amount = 10;
|
||||
long initialBalance = getBalance(inventoryDataSource, productId);
|
||||
try {
|
||||
application.placeOrder(productId, amount);
|
||||
} catch (Exception e) {
|
||||
System.out.println(e.getMessage());
|
||||
}
|
||||
long finalBalance = getBalance(inventoryDataSource, productId);
|
||||
assertEquals(initialBalance, finalBalance);
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setUp() throws SQLException {
|
||||
|
||||
Connection inventoryConnection = inventoryDataSource.getConnection();
|
||||
Connection orderConnection = orderDataSource.getConnection();
|
||||
String createInventoryTable = "create table Inventory ( " + " productId VARCHAR ( 100 ) PRIMARY KEY, balance INT )";
|
||||
String createInventoryRow = "insert into Inventory values ( '" + productId + "', 10000 )";
|
||||
Statement s1 = inventoryConnection.createStatement();
|
||||
try {
|
||||
s1.executeUpdate(createInventoryTable);
|
||||
} catch (Exception e) {
|
||||
System.out.println("Inventory table exists");
|
||||
}
|
||||
try {
|
||||
s1.executeUpdate(createInventoryRow);
|
||||
} catch (Exception e) {
|
||||
System.out.println("Product row exists");
|
||||
}
|
||||
s1.close();
|
||||
String createOrderTable = "create table Orders ( orderId VARCHAR ( 100 ) PRIMARY KEY, productId VARCHAR ( 100 ), amount INT NOT NULL CHECK (amount <= 5) )";
|
||||
Statement s2 = orderConnection.createStatement();
|
||||
try {
|
||||
s2.executeUpdate(createOrderTable);
|
||||
} catch (Exception e) {
|
||||
System.out.println("Orders table exists");
|
||||
}
|
||||
s2.close();
|
||||
inventoryConnection.close();
|
||||
orderConnection.close();
|
||||
}
|
||||
|
||||
private static long getBalance(DataSource inventoryDataSource, String productId) throws Exception {
|
||||
|
||||
Connection inventoryConnection = inventoryDataSource.getConnection();
|
||||
Statement s1 = inventoryConnection.createStatement();
|
||||
String q1 = "select balance from Inventory where productId='" + productId + "'";
|
||||
ResultSet rs1 = s1.executeQuery(q1);
|
||||
if (rs1 == null || !rs1.next())
|
||||
throw new Exception("Product not found: " + productId);
|
||||
long balance = rs1.getLong(1);
|
||||
inventoryConnection.close();
|
||||
return balance;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+80
@@ -0,0 +1,80 @@
|
||||
package com.baeldung.atomikos.spring.jpa;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import com.baeldung.atomikos.spring.jpa.config.Config;
|
||||
import com.baeldung.atomikos.spring.jpa.inventory.Inventory;
|
||||
import com.baeldung.atomikos.spring.jpa.inventory.InventoryConfig;
|
||||
import com.baeldung.atomikos.spring.jpa.inventory.InventoryRepository;
|
||||
import com.baeldung.atomikos.spring.jpa.order.OrderConfig;
|
||||
import com.baeldung.atomikos.spring.jpa.order.OrderRepository;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = { Config.class, InventoryConfig.class, OrderConfig.class })
|
||||
public class ApplicationUnitTest {
|
||||
|
||||
private static String productId = UUID.randomUUID()
|
||||
.toString();
|
||||
|
||||
@Autowired
|
||||
Application application;
|
||||
|
||||
@Autowired
|
||||
InventoryRepository inventoryRepository;
|
||||
|
||||
@Autowired
|
||||
OrderRepository orderRepository;
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void testPlaceOrderSuccess() throws Exception {
|
||||
int amount = 1;
|
||||
long initialBalance = getBalance(inventoryRepository, productId);
|
||||
application.placeOrder(productId, amount);
|
||||
long finalBalance = getBalance(inventoryRepository, productId);
|
||||
assertEquals(initialBalance - amount, finalBalance);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void testPlaceOrderFailure() throws Exception {
|
||||
int amount = 10;
|
||||
long initialBalance = getBalance(inventoryRepository, productId);
|
||||
try {
|
||||
application.placeOrder(productId, amount);
|
||||
} catch (Exception e) {
|
||||
System.out.println(e.getMessage());
|
||||
}
|
||||
long finalBalance = getBalance(inventoryRepository, productId);
|
||||
assertEquals(initialBalance, finalBalance);
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setUp() throws SQLException {
|
||||
|
||||
Inventory inventory = new Inventory();
|
||||
inventory.setProductId(productId);
|
||||
inventory.setBalance(new Long(10000));
|
||||
inventoryRepository.save(inventory);
|
||||
|
||||
}
|
||||
|
||||
private static long getBalance(InventoryRepository inventoryRepository, String productId) throws Exception {
|
||||
|
||||
return inventoryRepository.findOne(productId)
|
||||
.getBalance();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
||||
@@ -0,0 +1 @@
|
||||
com.atomikos.icatch.file=logs
|
||||
@@ -162,7 +162,7 @@
|
||||
<properties>
|
||||
<datasource-proxy.version>1.6</datasource-proxy.version>
|
||||
<hibernate-types.version>2.9.7</hibernate-types.version>
|
||||
<hibernate.version>5.4.14.Final</hibernate.version>
|
||||
<hibernate.version>5.6.15.Final</hibernate.version>
|
||||
<javassist.version>3.27.0-GA</javassist.version>
|
||||
<jaxb.version>2.3.1</jaxb.version>
|
||||
<log4jdbc.version>2.0.0</log4jdbc.version>
|
||||
|
||||
@@ -37,7 +37,7 @@
|
||||
|
||||
<properties>
|
||||
<mysql-connector-java.version>8.2.0</mysql-connector-java.version>
|
||||
<liquibase-maven-plugin.version>3.4.2</liquibase-maven-plugin.version>
|
||||
<liquibase-maven-plugin.version>4.25.0</liquibase-maven-plugin.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@@ -18,6 +18,7 @@
|
||||
<module>apache-bookkeeper</module>
|
||||
<module>apache-cayenne</module>
|
||||
<module>apache-derby</module>
|
||||
<module>atomikos</module>
|
||||
<module>blaze-persistence</module>
|
||||
<module>core-java-persistence</module>
|
||||
<module>core-java-persistence-2</module>
|
||||
|
||||
@@ -4,8 +4,7 @@ This module contains articles about Spring Data JPA used in enterprise applicati
|
||||
|
||||
### Relevant Articles:
|
||||
|
||||
- [Spring JPA – Multiple Databases](https://www.baeldung.com/spring-data-jpa-multiple-databases)
|
||||
- [Pagination and Sorting using Spring Data JPA](https://www.baeldung.com/spring-data-jpa-pagination-sorting)
|
||||
|
||||
|
||||
### Eclipse Config
|
||||
After importing the project into Eclipse, you may see the following error:
|
||||
|
||||
@@ -11,6 +11,8 @@ This module contains articles about Spring Data JPA used in enterprise applicati
|
||||
- [Custom Naming Convention with Spring Data JPA](https://www.baeldung.com/spring-data-jpa-custom-naming)
|
||||
- [Partial Data Update With Spring Data](https://www.baeldung.com/spring-data-partial-update)
|
||||
- [Spring Data JPA @Modifying Annotation](https://www.baeldung.com/spring-data-jpa-modifying-annotation)
|
||||
- [Spring JPA – Multiple Databases](https://www.baeldung.com/spring-data-jpa-multiple-databases)
|
||||
- [Pagination and Sorting using Spring Data JPA](https://www.baeldung.com/spring-data-jpa-pagination-sorting)
|
||||
|
||||
### Eclipse Config
|
||||
After importing the project into Eclipse, you may see the following error:
|
||||
|
||||
+5
-3
@@ -1,6 +1,9 @@
|
||||
package com.baeldung.multipledb;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
import java.util.HashMap;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
@@ -14,8 +17,7 @@ import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
|
||||
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
|
||||
import org.springframework.transaction.PlatformTransactionManager;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.util.HashMap;
|
||||
import com.google.common.base.Preconditions;
|
||||
|
||||
@Configuration
|
||||
@PropertySource({"classpath:persistence-multiple-db.properties"})
|
||||
+10
-4
@@ -1,8 +1,15 @@
|
||||
package com.baeldung.multipledb;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
import java.util.HashMap;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.*;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
import org.springframework.context.annotation.Profile;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
||||
import org.springframework.jdbc.datasource.DriverManagerDataSource;
|
||||
@@ -11,8 +18,7 @@ import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
|
||||
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
|
||||
import org.springframework.transaction.PlatformTransactionManager;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.util.HashMap;
|
||||
import com.google.common.base.Preconditions;
|
||||
|
||||
@Configuration
|
||||
@PropertySource({"classpath:persistence-multiple-db.properties"})
|
||||
+5
-1
@@ -1,6 +1,10 @@
|
||||
package com.baeldung.multipledb.model.user;
|
||||
|
||||
import javax.persistence.*;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@Table
|
||||
+8
-2
@@ -1,9 +1,15 @@
|
||||
package com.baeldung.multipledb.model.user;
|
||||
|
||||
import javax.persistence.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.OneToMany;
|
||||
import javax.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@Table(name = "users")
|
||||
public class User {
|
||||
@@ -2,9 +2,7 @@
|
||||
|
||||
### Relevant Articles:
|
||||
|
||||
- [Introduction to Spring Data JPA](https://www.baeldung.com/the-persistence-layer-with-spring-data-jpa)
|
||||
- [Performance Difference Between save() and saveAll() in Spring Data](https://www.baeldung.com/spring-data-save-saveall)
|
||||
- [LIKE Queries in Spring JPA Repositories](https://www.baeldung.com/spring-jpa-like-queries)
|
||||
- [How to Access EntityManager with Spring Data](https://www.baeldung.com/spring-data-entitymanager)
|
||||
- [Difference Between JPA and Spring Data JPA](https://www.baeldung.com/spring-data-jpa-vs-jpa)
|
||||
- [Differences Between Spring Data JPA findFirst() and findTop()](https://www.baeldung.com/spring-data-jpa-findfirst-vs-findtop)
|
||||
|
||||
@@ -42,11 +42,6 @@
|
||||
<groupId>com.querydsl</groupId>
|
||||
<artifactId>querydsl-jpa</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.guava</groupId>
|
||||
<artifactId>guava</artifactId>
|
||||
<version>${guava.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
||||
-5
@@ -1,5 +0,0 @@
|
||||
package com.baeldung.spring.data.persistence.repository;
|
||||
|
||||
public interface IFooService {
|
||||
Foo create(Foo foo);
|
||||
}
|
||||
@@ -1 +0,0 @@
|
||||
DELETE FROM "movie";
|
||||
@@ -1,7 +0,0 @@
|
||||
INSERT INTO "movie" ("id", "title", "director", "rating", "duration") VALUES(1, 'Godzilla: King of the Monsters', ' Michael Dougherty', 'PG-13', 132);
|
||||
INSERT INTO "movie" ("id", "title", "director", "rating", "duration") VALUES(2, 'Avengers: Endgame', 'Anthony Russo', 'PG-13', 181);
|
||||
INSERT INTO "movie" ("id", "title", "director", "rating", "duration") VALUES(3, 'Captain Marvel', 'Anna Boden', 'PG-13', 123);
|
||||
INSERT INTO "movie" ("id", "title", "director", "rating", "duration") VALUES(4, 'Dumbo', 'Tim Burton', 'PG', 112);
|
||||
INSERT INTO "movie" ("id", "title", "director", "rating", "duration") VALUES(5, 'Booksmart', 'Olivia Wilde', 'R', 102);
|
||||
INSERT INTO "movie" ("id", "title", "director", "rating", "duration") VALUES(6, 'Aladdin', 'Guy Ritchie', 'PG', 128);
|
||||
INSERT INTO "movie" ("id", "title", "director", "rating", "duration") VALUES(7, 'The Sun Is Also a Star', 'Ry Russo-Young', 'PG-13', 100);
|
||||
@@ -3,6 +3,7 @@
|
||||
This module contains articles about repositories in Spring Data JPA
|
||||
|
||||
### Relevant Articles:
|
||||
- [Introduction to Spring Data JPA](https://www.baeldung.com/the-persistence-layer-with-spring-data-jpa)
|
||||
- [Case Insensitive Queries with Spring Data Repository](https://www.baeldung.com/spring-data-case-insensitive-queries)
|
||||
- [Derived Query Methods in Spring Data JPA Repositories](https://www.baeldung.com/spring-data-derived-queries)
|
||||
- [Spring Data – CrudRepository save() Method](https://www.baeldung.com/spring-data-crud-repository-save)
|
||||
@@ -10,6 +11,8 @@ This module contains articles about repositories in Spring Data JPA
|
||||
- [Spring Data Composable Repositories](https://www.baeldung.com/spring-data-composable-repositories)
|
||||
- [Spring Data JPA Repository Populators](https://www.baeldung.com/spring-data-jpa-repository-populators)
|
||||
- [Calling Stored Procedures from Spring Data JPA Repositories](https://www.baeldung.com/spring-data-jpa-stored-procedures)
|
||||
- [LIKE Queries in Spring JPA Repositories](https://www.baeldung.com/spring-jpa-like-queries)
|
||||
|
||||
- More articles: [[--> next]](../spring-data-jpa-repo-2)
|
||||
|
||||
### Eclipse Config
|
||||
|
||||
@@ -42,6 +42,11 @@
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-oxm</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.guava</groupId>
|
||||
<artifactId>guava</artifactId>
|
||||
<version>${guava.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
+7
-2
@@ -1,8 +1,13 @@
|
||||
package com.baeldung.spring.data.persistence.repository;
|
||||
package com.baeldung.repository;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.io.Serializable;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
|
||||
@Entity
|
||||
public class Foo implements Serializable {
|
||||
@Id
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.spring.data.persistence.repository;
|
||||
package com.baeldung.repository;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
+2
-1
@@ -1,8 +1,9 @@
|
||||
package com.baeldung.spring.data.persistence.repository;
|
||||
package com.baeldung.repository;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
public interface IFooDAO extends JpaRepository<Foo, Long> {
|
||||
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
package com.baeldung.repository;
|
||||
|
||||
public interface IFooService {
|
||||
Foo create(Foo foo);
|
||||
}
|
||||
+8
-6
@@ -1,6 +1,9 @@
|
||||
package com.baeldung.spring.data.persistence.repository;
|
||||
package com.baeldung.repository;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
@@ -16,15 +19,14 @@ import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
|
||||
import org.springframework.transaction.PlatformTransactionManager;
|
||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.util.Properties;
|
||||
import com.google.common.base.Preconditions;
|
||||
|
||||
@Configuration
|
||||
@PropertySource("classpath:persistence.properties")
|
||||
@ComponentScan("com.baeldung.spring.data.persistence.repository")
|
||||
@ComponentScan("com.baeldung.repository")
|
||||
//@ImportResource("classpath*:*springDataConfig.xml")
|
||||
@EnableTransactionManagement
|
||||
@EnableJpaRepositories(basePackages = "com.baeldung.spring.data.persistence.repository")
|
||||
@EnableJpaRepositories(basePackages = "com.baeldung.repository")
|
||||
public class PersistenceConfig {
|
||||
|
||||
@Autowired
|
||||
+3
-2
@@ -1,11 +1,12 @@
|
||||
package com.baeldung.spring.data.persistence.like.repository;
|
||||
|
||||
import com.baeldung.spring.data.persistence.like.model.Movie;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
|
||||
import java.util.List;
|
||||
import com.baeldung.spring.data.persistence.like.model.Movie;
|
||||
|
||||
public interface MovieRepository extends CrudRepository<Movie, Long> {
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
# jdbc.X
|
||||
jdbc.driverClassName=org.h2.Driver
|
||||
jdbc.url=jdbc:h2:mem:db;DB_CLOSE_DELAY=-1
|
||||
jdbc.user=sa
|
||||
jdbc.pass=sa
|
||||
|
||||
# hibernate.X
|
||||
hibernate.hbm2ddl.auto=create-drop
|
||||
hibernate.dialect=org.hibernate.dialect.H2Dialect
|
||||
+1
-1
@@ -7,5 +7,5 @@
|
||||
http://www.springframework.org/schema/data/jpa
|
||||
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd"
|
||||
>
|
||||
<jpa:repositories base-package="com.baeldung.spring.data.persistence.repository"/>
|
||||
<jpa:repositories base-package="com.baeldung.repository"/>
|
||||
</beans>
|
||||
+3
-3
@@ -1,4 +1,6 @@
|
||||
package com.baeldung.spring.data.persistence.repository;
|
||||
package com.baeldung.repository;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
@@ -7,8 +9,6 @@ import org.springframework.dao.DataIntegrityViolationException;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@ContextConfiguration(classes = PersistenceConfig.class)
|
||||
public class FooServiceIntegrationTest {
|
||||
+9
-7
@@ -1,7 +1,12 @@
|
||||
package com.baeldung.spring.data.persistence.like;
|
||||
|
||||
import com.baeldung.spring.data.persistence.like.model.Movie;
|
||||
import com.baeldung.spring.data.persistence.like.repository.MovieRepository;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.springframework.test.context.jdbc.Sql.ExecutionPhase.AFTER_TEST_METHOD;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -9,11 +14,8 @@ import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.jdbc.Sql;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.springframework.test.context.jdbc.Sql.ExecutionPhase.AFTER_TEST_METHOD;
|
||||
import com.baeldung.spring.data.persistence.like.model.Movie;
|
||||
import com.baeldung.spring.data.persistence.like.repository.MovieRepository;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@Sql(scripts = { "/test-movie-data.sql" })
|
||||
+2
-2
@@ -1,2 +1,2 @@
|
||||
spring.jpa.hibernate.ddl-auto=update
|
||||
spring.datasource.url=jdbc:h2:mem:jpa3
|
||||
#spring.jpa.hibernate.ddl-auto=update
|
||||
#spring.datasource.url=jdbc:h2:mem:jpa3
|
||||
@@ -0,0 +1 @@
|
||||
DELETE FROM movie;
|
||||
@@ -0,0 +1,7 @@
|
||||
INSERT INTO movie(id,title,director,rating,duration) VALUES(1, 'Godzilla: King of the Monsters', ' Michael Dougherty', 'PG-13', 132);
|
||||
INSERT INTO movie(id,title,director,rating,duration) VALUES(2, 'Avengers: Endgame', 'Anthony Russo', 'PG-13', 181);
|
||||
INSERT INTO movie(id,title,director,rating,duration) VALUES(3, 'Captain Marvel', 'Anna Boden', 'PG-13', 123);
|
||||
INSERT INTO movie(id,title,director,rating,duration) VALUES(4, 'Dumbo', 'Tim Burton', 'PG', 112);
|
||||
INSERT INTO movie(id,title,director,rating,duration) VALUES(5, 'Booksmart', 'Olivia Wilde', 'R', 102);
|
||||
INSERT INTO movie(id,title,director,rating,duration) VALUES(6, 'Aladdin', 'Guy Ritchie', 'PG', 128);
|
||||
INSERT INTO movie(id,title,director,rating,duration) VALUES(7, 'The Sun Is Also a Star', 'Ry Russo-Young', 'PG-13', 100);
|
||||
@@ -131,7 +131,7 @@
|
||||
<org.springframework.data.version>1.10.6.RELEASE</org.springframework.data.version>
|
||||
<org.springframework.security.version>4.2.1.RELEASE</org.springframework.security.version>
|
||||
<!-- persistence -->
|
||||
<hibernate.version>5.2.10.Final</hibernate.version>
|
||||
<hibernate.version>5.6.15.Final</hibernate.version>
|
||||
<hibernatesearch.version>5.8.2.Final</hibernatesearch.version>
|
||||
<mysql-connector-java.version>8.2.0</mysql-connector-java.version>
|
||||
<tomcat-dbcp.version>9.0.0.M26</tomcat-dbcp.version>
|
||||
|
||||
Reference in New Issue
Block a user