Skip to content

Commit

Permalink
Merge pull request #369 from speedment/jpa-inheritance
Browse files Browse the repository at this point in the history
Improve handling of JPA inheritance and annotation processing
  • Loading branch information
julgus committed Aug 29, 2023
2 parents aa6c041 + 4f7edd9 commit e6e2d4c
Show file tree
Hide file tree
Showing 49 changed files with 905 additions and 106 deletions.
37 changes: 14 additions & 23 deletions integration-tests/pom.xml
Expand Up @@ -17,7 +17,7 @@
<parent>
<artifactId>jpastreamer-parent</artifactId>
<groupId>com.speedment.jpastreamer</groupId>
<version>3.0.3-SNAPSHOT</version>
<version>3.0.4-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand All @@ -26,60 +26,51 @@
<name>Maven</name>
<url>http://maven.apache.org/</url>
<inceptionYear>2001</inceptionYear>

<distributionManagement>
<site>
<id>website</id>
<url>scp://webhost.company.com/www/website</url>
</site>
</distributionManagement>


<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>

<dependency>
<groupId>org.hibernate</groupId>
<groupId>org.hibernate.orm</groupId>
<artifactId>hibernate-core</artifactId>
<version>6.0.2.Final</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.hibernate</groupId>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.0.13.Final</version>
<version>8.0.1.Final</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
<version>1.0.2</version>
<scope>test</scope>
<groupId>jakarta.el</groupId>
<artifactId>jakarta.el-api</artifactId>
<version>5.0.1</version>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>jakarta.el</artifactId>
<version>5.0.0-M1</version>
</dependency>

<dependency>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy</artifactId>
<version>1.12.12</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.30</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.speedment.jpastreamer</groupId>
<artifactId>jpastreamer-core</artifactId>
<version>3.0.3-SNAPSHOT</version>
<scope>test</scope>
<version>${jpa-streamer.version}</version>
</dependency>

</dependencies>
Expand Down
@@ -0,0 +1,4 @@
MYSQL_ROOT_PASSWORD=password
MYSQL_DATABASE=testdb
MYSQL_USER=speedment
MYSQL_PASSWORD=password
@@ -0,0 +1,85 @@
package com.speedment.jpastreamer.integration.test.inheritance;

import com.speedment.jpastreamer.application.JPAStreamer;
import com.speedment.jpastreamer.integration.test.inheritance.model.BlogPost;
import com.speedment.jpastreamer.integration.test.inheritance.model.BlogPost$;
import com.speedment.jpastreamer.integration.test.inheritance.model.Book;
import com.speedment.jpastreamer.integration.test.inheritance.model.Book$;
import org.junit.jupiter.api.Test;

import java.util.List;
import java.util.stream.Collectors;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class InheritanceTest {

final static protected JPAStreamer jpaStreamer = JPAStreamer.of("testdb");

@Test
void countTest() {

final List<Book> collect = jpaStreamer.stream(Book.class).collect(Collectors.toList());

final long expected = collect.stream()
.filter(b -> b.getPages() > 300)
.count();

final long actual = jpaStreamer.stream(Book.class)
.filter(Book$.pages.greaterThan(300))
.count();

assertEquals(expected, actual);
}

@Test
void inheritanceTest() {

final List<Book> collect = jpaStreamer.stream(Book.class).collect(Collectors.toList());

final long expected = collect.stream()
.filter(b -> b.getTitle().contains("1"))
.count();

final long actual = jpaStreamer.stream(Book.class)
.filter(Book$.title.contains("1"))
.count();

assertEquals(expected, actual);
}

@Test
void inheritanceTest2() {

final List<BlogPost> collect = jpaStreamer.stream(BlogPost.class).collect(Collectors.toList());

final long expected = collect.stream()
.filter(b -> b.getTitle().contains("1"))
.count();

final long actual = jpaStreamer.stream(BlogPost.class)
.filter(BlogPost$.title.contains("1"))
.count();

assertEquals(expected, actual);
}

@Test
void inheritanceTest3() {

final List<BlogPost> collect = jpaStreamer.stream(BlogPost.class)
.collect(Collectors.toList());

final long expected = collect.stream()
.filter(b -> b.getTitle().contains("1"))
.count();

final long actual = jpaStreamer.stream(BlogPost.class)
.filter(BlogPost$.title.contains("1"))
.count();

assertEquals(expected, actual);
}


}
@@ -0,0 +1,20 @@
version: '3.8'

services:
test-db:
container_name: test-db
image: mysql:8.0
command: --default-authentication-plugin=mysql_native_password
restart: always
volumes:
- "./scripts/schema.sql:/docker-entrypoint-initdb.d/1.sql"
- "./scripts/data.sql:/docker-entrypoint-initdb.d/2.sql"
environment:
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
MYSQL_DATABASE: ${MYSQL_DATABASE}
MYSQL_USER: ${MYSQL_USER}
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
ports:
- "3305:3306"
expose:
- "3305"
@@ -0,0 +1,20 @@
package com.speedment.jpastreamer.integration.test.inheritance.model;

import jakarta.persistence.*;

@Entity
@Table(name = "blogpost", schema = "publications")
public class BlogPost extends Publication {

@Column(name = "url", nullable = false, updatable = false, columnDefinition = "varchar(255)")
private String url ;

public String getUrl() {
return url;
}

public void setUrl(String url) {
this.url = url;
}

}
@@ -0,0 +1,19 @@
package com.speedment.jpastreamer.integration.test.inheritance.model;

import jakarta.persistence.*;

@Entity
@Table(name = "books", schema = "publications")
public class Book extends Publication {

@Column(name = "pages", nullable = false, updatable = false, columnDefinition = "int(6)")
private Integer pages;

public Integer getPages() {
return pages;
}

public void setPages(Integer pages) {
this.pages = pages;
}
}
@@ -0,0 +1,56 @@
package com.speedment.jpastreamer.integration.test.inheritance.model;

import jakarta.persistence.*;

import java.time.LocalDateTime;

@MappedSuperclass
public abstract class Publication {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false, updatable = false, columnDefinition = "int(6)")
protected Integer id;

@Column(name = "publishing_date", nullable = false, updatable = false, columnDefinition = "date")
protected LocalDateTime publishingDate;

@Column(name = "title", nullable = false, updatable = false, columnDefinition = "varchar(255)")
private String title;

@Version
@Column(name = "version", nullable = false, updatable = false, columnDefinition = "int(6)")
private Integer version;

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public LocalDateTime getPublishingDate() {
return publishingDate;
}

public void setPublishingDate(LocalDateTime publishingDate) {
this.publishingDate = publishingDate;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public Integer getVersion() {
return version;
}

public void setVersion(Integer version) {
this.version = version;
}
}
@@ -0,0 +1,4 @@
#!/bin/sh

docker-compose rm -v -f -s test-db && docker-compose up -d
mysql -h 127.0.0.1 -P 3305 -u speedment -ppassword publications
@@ -0,0 +1,35 @@
INSERT INTO books (id, publishing_date, title, version, pages)
VALUES (1, '2008-7-04', 'Book 1', 2, 213),
(2, '2009-7-04', 'Book 2', 2, 234),
(3, '2010-7-04', 'Book 3', 2, 643),
(4, '2011-7-04', 'Book 4', 2, 211),
(5, '2012-7-04', 'Book 5', 2, 887),
(6, '2013-7-04', 'Book 6', 2, 123),
(7, '2014-7-04', 'Book 7', 2, 312),
(8, '2015-7-04', 'Book 8', 2, 11),
(9, '2016-7-04', 'Book 9', 2, 523),
(10, '2017-7-04', 'Book 10', 2, 432),
(11, '2018-7-04', 'Book 11', 2, 322),
(12, '2019-7-04', 'Book 12', 2, 121);

INSERT INTO blogposts (id, publishing_date, title, version, url)
VALUES (1, '2008-7-04', 'Blog Post 1', 2, 'http://speedment.com'),
(2, '2009-7-04', 'Blog Post 2', 2, 'http://speedment.com'),
(3, '2010-7-04', 'Blog Post 3', 2, 'http://speedment.com'),
(4, '2011-7-04', 'Blog Post 4', 2, 'http://speedment.com'),
(5, '2012-7-04', 'Blog Post 5', 2, 'http://speedment.com'),
(6, '2013-7-04', 'Blog Post 6', 2, 'http://speedment.com'),
(7, '2014-7-04', 'Blog Post 7', 2, 'http://speedment.com'),
(8, '2015-7-04', 'Blog Post 8', 2, 'http://speedment.com'),
(9, '2016-7-04', 'Blog Post 9', 2, 'http://speedment.com'),
(10, '2017-7-04', 'Blog Post 10', 2, 'http://speedment.com'),
(11, '2018-7-04', 'Blog Post 11', 2, 'http://speedment.com'),
(12, '2019-7-04', 'Blog Post 12', 2, 'http://speedment.com'),

INSERT INTO author (id, firstname, lastname, version)
(1, 'Author 1', 'Lastname', 2),
(2, 'Author 2', 'Blog Post 2', 2),
(3, 'Author 3', 'Blog Post 3', 2),
(4, 'Author 5', 'Blog Post 4', 2),
(5, 'Author 6', 'Blog Post 5', 2);

@@ -0,0 +1,25 @@
CREATE TABLE books (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
publishing_date DATE NOT NULL,
title VARCHAR(255) NOT NULL,
version INT(6),
pages INT(6),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE blogpost (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
publishing_date DATE NOT NULL,
title VARCHAR(255) NOT NULL,
version INT(6),
url VARCHAR(255) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE author (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(255) NOT NULL,
lastname VARCHAR(255) NOT NULL,
version INT(6),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
@@ -0,0 +1,4 @@
MYSQL_ROOT_PASSWORD=password
MYSQL_DATABASE=testdb2
MYSQL_USER=speedment
MYSQL_PASSWORD=password

0 comments on commit e6e2d4c

Please sign in to comment.