Skip to content

Commit 2844bbb

Browse files
Add files via upload
1 parent 827c17c commit 2844bbb

File tree

9 files changed

+339
-0
lines changed

9 files changed

+339
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<actions>
3+
<action>
4+
<actionName>run</actionName>
5+
<packagings>
6+
<packaging>jar</packaging>
7+
</packagings>
8+
<goals>
9+
<goal>spring-boot:run</goal>
10+
</goals>
11+
<properties>
12+
<spring-boot.run.jvmArguments>-noverify -XX:TieredStopAtLevel=1 </spring-boot.run.jvmArguments>
13+
<spring-boot.run.mainClass>com.jpa.JpaApplication</spring-boot.run.mainClass>
14+
<Env.SPRING_OUTPUT_ANSI_ENABLED>always</Env.SPRING_OUTPUT_ANSI_ENABLED>
15+
</properties>
16+
</action>
17+
<action>
18+
<actionName>debug</actionName>
19+
<packagings>
20+
<packaging>jar</packaging>
21+
</packagings>
22+
<goals>
23+
<goal>spring-boot:run</goal>
24+
</goals>
25+
<properties>
26+
<spring-boot.run.jvmArguments>-Xdebug -Xrunjdwp:transport=dt_socket,server=n,address=${jpda.address} -noverify -XX:TieredStopAtLevel=1 </spring-boot.run.jvmArguments>
27+
<spring-boot.run.mainClass>com.jpa.JpaApplication</spring-boot.run.mainClass>
28+
<Env.SPRING_OUTPUT_ANSI_ENABLED>always</Env.SPRING_OUTPUT_ANSI_ENABLED>
29+
<jpda.listen>true</jpda.listen>
30+
</properties>
31+
</action>
32+
<action>
33+
<actionName>profile</actionName>
34+
<packagings>
35+
<packaging>jar</packaging>
36+
</packagings>
37+
<goals>
38+
<goal>process-classes</goal>
39+
<goal>org.codehaus.mojo:exec-maven-plugin:1.2.1:exec</goal>
40+
</goals>
41+
<properties>
42+
<exec.args>-classpath %classpath com.jpa.JpaApplication</exec.args>
43+
<exec.executable>java</exec.executable>
44+
</properties>
45+
</action>
46+
</actions>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.jpa</groupId>
7+
<artifactId>jpa</artifactId>
8+
<version>1.0</version>
9+
<packaging>jar</packaging>
10+
11+
<name>jpa</name>
12+
<description>JPA project for Spring Boot</description>
13+
14+
<parent>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-parent</artifactId>
17+
<version>2.0.5.RELEASE</version>
18+
<relativePath/> <!-- lookup parent from repository -->
19+
</parent>
20+
21+
<properties>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
24+
<java.version>1.8</java.version>
25+
</properties>
26+
27+
<dependencies>
28+
<dependency>
29+
<groupId>org.springframework.boot</groupId>
30+
<artifactId>spring-boot-starter-data-jpa</artifactId>
31+
</dependency>
32+
<dependency>
33+
<groupId>org.springframework.boot</groupId>
34+
<artifactId>spring-boot-starter-jdbc</artifactId>
35+
</dependency>
36+
<dependency>
37+
<groupId>org.springframework.boot</groupId>
38+
<artifactId>spring-boot-starter-web</artifactId>
39+
</dependency>
40+
<dependency>
41+
<groupId>mysql</groupId>
42+
<artifactId>mysql-connector-java</artifactId>
43+
<scope>runtime</scope>
44+
</dependency>
45+
<dependency>
46+
<groupId>org.springframework.boot</groupId>
47+
<artifactId>spring-boot-starter-test</artifactId>
48+
<scope>test</scope>
49+
</dependency>
50+
</dependencies>
51+
52+
<build>
53+
<plugins>
54+
<plugin>
55+
<groupId>org.springframework.boot</groupId>
56+
<artifactId>spring-boot-maven-plugin</artifactId>
57+
</plugin>
58+
</plugins>
59+
</build>
60+
61+
62+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package com.jpa;
2+
3+
import java.io.Serializable;
4+
import javax.persistence.ColumnResult;
5+
import javax.persistence.ConstructorResult;
6+
import javax.persistence.Entity;
7+
import javax.persistence.GeneratedValue;
8+
import javax.persistence.GenerationType;
9+
import javax.persistence.Id;
10+
import javax.persistence.NamedNativeQuery;
11+
import javax.persistence.SqlResultSetMapping;
12+
import javax.persistence.Table;
13+
14+
@NamedNativeQuery(
15+
name="CarDto",
16+
query="select c.name as name, c.color as color from Car c",
17+
resultSetMapping="CarDto"
18+
)
19+
@SqlResultSetMapping(
20+
name="CarDto",
21+
classes=@ConstructorResult(
22+
targetClass=CarDto.class,
23+
columns={
24+
@ColumnResult(name="name"),
25+
@ColumnResult(name="color")
26+
}
27+
)
28+
)
29+
@Entity
30+
@Table(name = "car")
31+
public class Car implements Serializable {
32+
33+
private static final long serialVersionUID = 1L;
34+
35+
@Id
36+
@GeneratedValue(strategy = GenerationType.IDENTITY)
37+
private Long id;
38+
39+
private String name;
40+
private String engine;
41+
private String color;
42+
43+
public Long getId() {
44+
return id;
45+
}
46+
47+
public void setId(Long id) {
48+
this.id = id;
49+
}
50+
51+
public String getName() {
52+
return name;
53+
}
54+
55+
public void setName(String name) {
56+
this.name = name;
57+
}
58+
59+
public String getEngine() {
60+
return engine;
61+
}
62+
63+
public void setEngine(String engine) {
64+
this.engine = engine;
65+
}
66+
67+
public String getColor() {
68+
return color;
69+
}
70+
71+
public void setColor(String color) {
72+
this.color = color;
73+
}
74+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.jpa;
2+
3+
import java.io.Serializable;
4+
5+
public class CarDto implements Serializable {
6+
7+
private static final long serialVersionUID = 1L;
8+
9+
private final String name;
10+
private final String color;
11+
12+
public CarDto(String name, String color) {
13+
this.name = name;
14+
this.color = color;
15+
}
16+
17+
public String getName() {
18+
return name;
19+
}
20+
21+
public String getColor() {
22+
return color;
23+
}
24+
25+
@Override
26+
public String toString() {
27+
return "CarDto{" + "name=" + name + ", color=" + color + '}';
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.jpa;
2+
3+
import java.util.List;
4+
import org.springframework.stereotype.Service;
5+
import org.springframework.transaction.annotation.Transactional;
6+
7+
@Service
8+
public class CarService {
9+
10+
private final Dao dao;
11+
12+
public CarService(Dao dao) {
13+
this.dao = dao;
14+
}
15+
16+
@Transactional
17+
public void populateCars() {
18+
19+
Car car_1=new Car();
20+
car_1.setName("Dacia");
21+
car_1.setEngine("V8");
22+
car_1.setColor("red");
23+
24+
Car car_2=new Car();
25+
car_2.setName("BMW");
26+
car_2.setEngine("V6");
27+
car_2.setColor("blue");
28+
29+
dao.persist(car_1);
30+
dao.persist(car_2);
31+
}
32+
33+
public List<CarDto> fetchCars() {
34+
return dao.fetchCars();
35+
}
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.jpa;
2+
3+
import java.io.Serializable;
4+
import java.util.List;
5+
import java.util.Objects;
6+
import javax.persistence.EntityManager;
7+
import javax.persistence.PersistenceContext;
8+
import javax.persistence.Query;
9+
import org.springframework.stereotype.Repository;
10+
import org.springframework.transaction.annotation.Transactional;
11+
12+
@Repository
13+
@Transactional
14+
public class Dao<T, ID extends Serializable> implements GenericDao<T, ID> {
15+
16+
@PersistenceContext
17+
private EntityManager entityManager;
18+
19+
@Override
20+
public <S extends T> S persist(S entity) {
21+
22+
Objects.requireNonNull(entity, "Cannot persist a null entity");
23+
24+
entityManager.persist(entity);
25+
26+
return entity;
27+
}
28+
29+
@Transactional(readOnly=true)
30+
public List<CarDto> fetchCars() {
31+
Query query = entityManager.createNamedQuery("CarDto");
32+
List<CarDto> result = query.getResultList();
33+
34+
return result;
35+
}
36+
37+
protected EntityManager getEntityManager() {
38+
return entityManager;
39+
}
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.jpa;
2+
3+
import java.util.List;
4+
import java.util.logging.Logger;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.boot.ApplicationRunner;
7+
import org.springframework.boot.SpringApplication;
8+
import org.springframework.boot.autoconfigure.SpringBootApplication;
9+
import org.springframework.context.annotation.Bean;
10+
11+
@SpringBootApplication
12+
public class DtoViaSqlResultSetMappingAndNamedNativeQueryApplication {
13+
14+
private static final Logger logger
15+
= Logger.getLogger(DtoViaSqlResultSetMappingAndNamedNativeQueryApplication.class.getName());
16+
17+
@Autowired
18+
private CarService carService;
19+
20+
public static void main(String[] args) {
21+
SpringApplication.run(DtoViaSqlResultSetMappingAndNamedNativeQueryApplication.class, args);
22+
}
23+
24+
@Bean
25+
public ApplicationRunner init() {
26+
return args -> {
27+
28+
carService.populateCars();
29+
List<CarDto> cars = carService.fetchCars();
30+
31+
cars.forEach((e) -> logger.info(() -> "Car: " + e));
32+
};
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.jpa;
2+
3+
import java.io.Serializable;
4+
import java.util.List;
5+
6+
public interface GenericDao<T, ID extends Serializable> {
7+
8+
<S extends T> S persist(S entity);
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
spring.datasource.url=jdbc:mysql://localhost:3306/db_cars?createDatabaseIfNotExist=true
2+
spring.datasource.username=root
3+
spring.datasource.password=root
4+
5+
spring.jpa.hibernate.ddl-auto=create
6+
spring.jpa.show-sql=true
7+
8+
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
9+

0 commit comments

Comments
 (0)