Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

147 Deprecated JPA Exceptions #249

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
- ruleID: spring-framework-5.x-to-6.0-data-access-00040
category: optional
effort: 1
labels:
- konveyor.io/source=spring5
- konveyor.io/target=spring6+
tag:
- Spring Framework
when:
java.referenced:
pattern: org.springframework.dao.DeadlockLoserDataAccessException
location: IMPORT
description: DeadlockLoserDataAccessException is deprecated in favor of CannotAcquireLockException
message: "DeadlockLoserDataAccessException is deprecated in favor of CannotAcquireLockException"
links:
- title: 'org.springframework.dao.DeadlockLoserDataAccessException'
url: https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/dao/DeadlockLoserDataAccessException.html

- ruleID: spring-framework-5.x-to-6.0-data-access-00050
category: optional
effort: 1
labels:
- konveyor.io/source=spring5
- konveyor.io/target=spring6+
tag:
- Spring Framework
when:
java.referenced:
pattern: org.springframework.dao.CannotSerializeTransactionException
location: IMPORT
description: CannotSerializeTransactionException is deprecated in favor of CannotAcquireLockException
message: "CannotSerializeTransactionException is deprecated in favor of CannotAcquireLockException"
links:
- title: 'org.springframework.dao.CannotSerailizeTransactionException Documentation'
url: https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/dao/CannotSerializeTransactionException.html
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>accessing-data-jpa-complete</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-dao</artifactId>
<version>2.0.8</version>
</dependency>

<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.accessingdatajpa;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.dao.DeadlockLoserDataAccessException;
import org.springframework.dao.CannotSerializeTransactionException;

@SpringBootApplication
public class AccessingDataJpaApplication {

private static final Logger log = LoggerFactory.getLogger(AccessingDataJpaApplication.class);

public static void main(String[] args) {
SpringApplication.run(AccessingDataJpaApplication.class);
}

@Bean
public CommandLineRunner demo(CustomerRepository repository) {
return (args) -> {
// save a few customers
repository.save(new Customer("Jack", "Bauer"));
repository.save(new Customer("Chloe", "O'Brian"));
repository.save(new Customer("Kim", "Bauer"));
repository.save(new Customer("David", "Palmer"));
repository.save(new Customer("Michelle", "Dessler"));
repository.save(new Customer("CannotSerializeTransaction", "Exception"));
repository.save(new Customer("DeadlockLoserDataAccess", "Exception"));

// fetch all customers
log.info("Customers found with findAll():");
log.info("-------------------------------");
repository.findAll().forEach(customer -> {
log.info(customer.toString());
try{
throwException("CannotSerializeTransaction", customer);
}catch(CannotSerializeTransactionException e){
log.error("CannotSerializeTransaction Exception thrown and caught");
}
try{
throwException("DeadlockLoserDataAccess", customer);
}catch(CannotSerializeTransactionException e){
log.error("CannotSerializeTransaction Exception thrown and caught");
}
});
// fetch an individual customer by ID
Customer customer = repository.findById(1L);
log.info("Customer found with findById(1L):");
log.info("--------------------------------");
log.info(customer.toString());
log.info("");

// fetch customers by last name
log.info("Customer found with findByLastName('Bauer'):");
log.info("--------------------------------------------");
repository.findByLastName("Bauer").forEach(bauer -> {
log.info(bauer.toString());
});
log.info("");
};
}

public void throwException(String exception, Customer customer){
if(customer.isException(exception)){
throw new CannotSerializeTransactionException("Exception");
}
else if(customer.isException(exception)){
throw new DeadlockLoserDataAccessException("Exception", new Throwable());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.accessingdatajpa;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class Customer {

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
private String firstName;
private String lastName;

protected Customer() {}

public Customer(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}

@Override
public String toString() {
return String.format(
"Customer[id=%d, firstName='%s', lastName='%s']",
id, firstName, lastName);
}

public boolean isException(String exceptionName){
if(lastName.equals("Exception") && firstName.equals(exceptionName)){
return true;
}
return false;
}

public Long getId() {
return id;
}

public String getFirstName() {
return firstName;
}

public String getLastName() {
return lastName;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.accessingdatajpa;

import java.util.List;

import org.springframework.data.repository.CrudRepository;

public interface CustomerRepository extends CrudRepository<Customer, Long> {

List<Customer> findByLastName(String lastName);

Customer findById(long id);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright 2002-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.accessingdatajpa;

import static org.assertj.core.api.Assertions.assertThat;

import java.util.List;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager;

import com.accessingdatajpa.Customer;
import com.accessingdatajpa.CustomerRepository;

@DataJpaTest
public class CustomerRepositoryTests {
@Autowired
private TestEntityManager entityManager;

@Autowired
private CustomerRepository customers;

@Test
public void testFindByLastName() {
Customer customer = new Customer("first", "last");
entityManager.persist(customer);

List<Customer> findByLastName = customers.findByLastName(customer.getLastName());

assertThat(findByLastName).extracting(Customer::getLastName).containsOnly(customer.getLastName());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
rulesPath: ../spring-framework-5.x-to-6.0-jpa-data-access.yaml
providers:
- name: java
dataPath: ./data/jpa-data-access
tests:
- ruleID: spring-framework-5.x-to-6.0-data-access-00040
testCases:
- name: tc-1
analysisParams:
mode: "source-only"
hasIncidents:
exactly: 1
- ruleID: spring-framework-5.x-to-6.0-data-access-00050
testCases:
- name: tc-1
analysisParams:
mode: "source-only"
hasIncidents:
exactly: 1
Loading