Giter Site home page Giter Site logo

vladmihalcea / high-performance-java-persistence Goto Github PK

View Code? Open in Web Editor NEW
1.3K 89.0 482.0 4.27 MB

The High-Performance Java Persistence book and video course code examples

License: Apache License 2.0

Java 99.80% PLpgSQL 0.20% Batchfile 0.01% Shell 0.01%

high-performance-java-persistence's Introduction

High-Performance Java Persistence

The High-Performance Java Persistence book and video course code examples. I wrote this article about this repository since it's one of the best way to test JDBC, JPA, Hibernate or even jOOQ code. Or, if you prefer videos, you can watch this presentation on YouTube.

Are you struggling with application performance issues?

Hypersistence Optimizer

Imagine having a tool that can automatically detect if you are using JPA and Hibernate properly. No more performance issues, no more having to spend countless hours trying to figure out why your application is barely crawling.

Imagine discovering early during the development cycle that you are using suboptimal mappings and entity relationships or that you are missing performance-related settings.

More, with Hypersistence Optimizer, you can detect all such issues during testing and make sure you don't deploy to production a change that will affect data access layer performance.

Hypersistence Optimizer is the tool you've been long waiting for!

Training

If you are interested in on-site training, I can offer you my High-Performance Java Persistence training which can be adapted to one, two or three days of sessions. For more details, check out my website.

Consulting

If you want me to review your application and provide insight into how you can optimize it to run faster, then check out my consulting page.

High-Performance Java Persistence Video Courses

If you want the fastest way to learn how to speed up a Java database application, then you should definitely enroll in my High-Performance Java Persistence video courses.

High-Performance Java Persistence Book

Or, if you prefer reading books, you are going to love my High-Performance Java Persistence book as well.

High-Performance Java Persistence book High-Performance Java Persistence video course

Java

All examples require at least Java 17 because of the awesome Text Blocks feature, which makes JPQL and SQL queries so much readable.

Maven

You need to use Maven 3.6.2 or newer to build the project.

IntelliJ IDEA

On IntelliJ IDEA, the project runs just fine. You will have to make sure to select Java 17 or newer.

Database setup

The project uses various database systems for integration testing, and you can configure the JDBC connection settings using the DatasourceProvider instances (e.g., PostgreSQLDataSourceProvider).

By default, without configuring any database explicitly, HSQLDB is used for testing.

However, since some integration tests are designed to work on specific relational databases, we will need to have those databases started prior to running those tests.

Therefore, when running a DB-specific test, this GitHub repository will execute the following steps:

  1. First, the test will try to find whether there's a local RDBMS it can use to run the test.
  2. If no local database is found, the integration tests will use Testcontainers to bootstrap a Docker container with the required Oracle, SQL Server, PostgreSQL, MySQL, MariaDB, YugabyteDB, or CockroachDB instance on demand.

While you don't need to install any database manually on your local OS, this is recommended since your tests will run much faster than if they used Testcontainers.

Manual Database configuration

  • PostgreSQL

    You can install PostgreSQL, and the password for the postgres user should be admin.

    Now you need to create a high_performance_java_persistence database.

  • Oracle

    You need to download and install Oracle XE

    Set the sys password to admin

    Connect to Oracle using the "sys as sysdba" user and create a new user:

      alter session set "_ORACLE_SCRIPT"=true;
    
      create user oracle identified by admin default tablespace users;
    
      grant dba to oracle;
    
      alter system set processes=1000 scope=spfile;
    
      alter system set sessions=1000 scope=spfile;
      
      ALTER PROFILE DEFAULT LIMIT PASSWORD_LIFE_TIME UNLIMITED;
    

    Open the C:\app\${user.name}\product\21c\homes\OraDB21Home1\network\admin folder where ${user.name} is your current Windows username.

    Locate the tnsnames.ora and listener.ora files and change the port from 1522 to 1521 if that's the case. If you made these modifications, you need to restart the OracleOraDB21Home1TNSListener and OracleServiceXE Windows services.

  • MySQL

    You should install MySQL 8, and the password for the mysql user should be admin.

    Now, you need to create a high_performance_java_persistence schema

    Besides having all privileges on this schema, the mysql user also requires select permission on mysql.PROC.

    If you don't have a mysql user created at database installation time, you can create one as follows:

    CREATE USER 'mysql'@'localhost';
    
    SET PASSWORD for 'mysql'@'localhost'='admin';
    
    GRANT ALL PRIVILEGES ON high_performance_java_persistence.* TO 'mysql'@'localhost';
    
    GRANT SELECT ON mysql.* TO 'mysql'@'localhost';
    
    FLUSH PRIVILEGES;
    
  • SQL Server

    You can install SQL Server Express Edition with Tools. Choose mixed mode authentication and set the sa user password to adm1n.

    Open SQL Server Configuration Manager -> SQL Server Network Configuration and enable Named Pipes and TCP

    In the right pane of the TCP/IP option, choose Properties, then IP Addresses and make sure you Enable all listed IP addresses. You need to blank the dynamic TCP port value and configure the static TCP port 1433 for all IPs.

    Open SQL Server Management Studio and create the high_performance_java_persistence database

Maven

To build the project, don't use install or package. Instead, just compile test classes like this:

mvnw clean test-compile

Or you can just run the build.bat or build.sh scripts which run the above Maven command.

Afterward, just pick one test from the IDE and run it individually.

Don't you run all tests at once (e.g. mvn clean test) because the test suite will take a very long time to complete.

So, run the test you are interested in individually.

Enjoy learning more about Java Persistence, Hibernate, and database systems!

high-performance-java-persistence's People

Contributors

brandstaetter avatar dependabot[bot] avatar hnatiukdm avatar hypersistence avatar jlmc avatar marschall avatar nasky987 avatar rajadilipkolli avatar ttddyy avatar vladmihalcea avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

high-performance-java-persistence's Issues

Oracle schema or MSSQL Schema?

There's a file in your repository that references both the Oracle and MS SQL dialects:
https://github.com/vladmihalcea/high-performance-java-persistence/blob/master/jooq/jooq-mssql/src/test/resources/oracle/initial_schema.sql

From the contents, I suspect it is MS SQL, as Oracle doesn't have BIGINT or DATETIME2 types... This looks more like Oracle syntax: https://github.com/vladmihalcea/high-performance-java-persistence/blob/master/jooq/jooq-oracle/src/test/resources/oracle/initial_schema.sql

ResourceLocalDelayConnectionAcquisitionTest should not depend on oracle.xml.parser.v2.XMLElement.XMLElement

Hi Vlad

ResourceLocalDelayConnectionAcquisitionTest depends on XMLElement (oracle.xml.parser.v2.XMLElement.XMLElement) which seems to be part of the oracle XDK tools. If you don't have them installed, you can't build the project (see part of the stack trace below).

Looks like XDK tools don't come via maven central and need some process of installation. I did not invest a lot of time, but it's not simply straight forward to get hold of this class. Not sure if it's a good idea to depend on this class. You might want to consider replacing that class with something standard if feasibly possible.

Thanks
Urs

[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] /home/urs/work/git/other/high-performance-java-persistence/core/src/test/java/com/vladmihalcea/book/hpjp/hibernate/connection/ResourceLocalDelayConnectionAcquisitionTest.java:[31,28] package oracle.xml.parser.v2 does not exist
[ERROR] /home/urs/work/git/other/high-performance-java-persistence/core/src/test/java/com/vladmihalcea/book/hpjp/hibernate/connection/ResourceLocalDelayConnectionAcquisitionTest.java:[167,41] cannot find symbol
  symbol:   class XMLElement
  location: class com.vladmihalcea.book.hpjp.hibernate.connection.ResourceLocalDelayConnectionAcquisitionTest
[INFO] 2 errors 
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO] 
[INFO] high-performance-java-persistence .................. SUCCESS [  1.709 s]
[INFO] high-performance-java-persistence-core ............. FAILURE [  6.947 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 8.832 s
[INFO] Finished at: 2017-05-21T08:39:56+02:00
[INFO] Final Memory: 32M/251M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.2:testCompile (default-testCompile) on project high-performance-java-persistence-core: Compilation failure: Compilation failure:
[ERROR] /home/urs/work/git/other/high-performance-java-persistence/core/src/test/java/com/vladmihalcea/book/hpjp/hibernate/connection/ResourceLocalDelayConnectionAcquisitionTest.java:[31,28] package oracle.xml.parser.v2 does not exist
[ERROR] /home/urs/work/git/other/high-performance-java-persistence/core/src/test/java/com/vladmihalcea/book/hpjp/hibernate/connection/ResourceLocalDelayConnectionAcquisitionTest.java:[167,41] cannot find symbol
[ERROR] symbol:   class XMLElement
[ERROR] location: class com.vladmihalcea.book.hpjp.hibernate.connection.ResourceLocalDelayConnectionAcquisitionTest
[ERROR] -> [Help 1]

Set source code encoding to UTF-8

In case you got this error
Error: unmappable character ... for encoding US-ASCII
when:
mvn clean test-compile

the only solution I found was to setup the UTF-8 into the
/high-performance-java-persistence-master/core/pom.xml
like this:

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

Book typo

Hi @vladmihalcea

Not sure it belongs here but didn't find a better place to report this..
There is a typo in the book on page 221 for the title of figure 10.15.

It is written

The unidirectional @ManyToMany relationship

But should be

The multidirectional @ManyToMany relationship

Problem with the HSQL database

I tried to run a Junit test with "mvn clean package", but I get the following error:

WARN [Alice]: o.h.t.s.i.ExceptionHandlerLoggedImpl - GenerationTarget encountered exception acc
org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL via JDBC Statement

aused by: java.sql.SQLSyntaxErrorException: user lacks privilege or object not found: PUBLIC.POSTTAG

Could you please tell me, how can I initialize the HSQL database?

Thank you!

Best regards
Alexandru Jecan

Source code does not match book

In the book, you say that this repo contains all the source, however that seems not to be the case at all. The code used in the section 10.5.3 is missing, and the text appears to omit some files. Is there a different repo with ALL the source?

Support for Hibernate Search

Hi i am using json type column in one of my entity and i tried to index this entity in elastic search through hibernate search, but this column is not indexed, is this library supports hibernate search.

some dependencies are not in maven central repo

Compilation fails because of

[WARNING] The POM for com.oracle:ojdbc7_g:jar:12.1.0.1 is missing, no dependency information available
[WARNING] The POM for com.microsoft.sqlserver:sqljdbc4:jar:4.0 is missing, no dependency information available

Would it be possible to add suitable repositories to the base pom.xml?

Set<Long> myfield is mapped as Set<Integer>

I have a class that contains a field with type List<Long> myfield
It maps to MySQL 5.7 as a json column. But when I read it out from Spring Data JPA, it becomes Set<Integer>, and had type cast errors when using it downstream. Please help..

MariaDB Sequence Identifier Test-GlobalIdentifierGeneratorScopeTest.java

Hi Vlad:
In your book( High-Performance-java-persistence) page 170, 9.2.3.2 Sequence Generator and 9.2.3. Numerical Identifiers, which imply MYSQL 5.7 doesn't support Sequence Generator. MariaDB 10.3 starts support for CREATE SEQUENCE ( https://mariadb.com/kb/en/library/create-sequence/).

I would like to use GlobalIdentifierGeneratorScopeTest.java as template to test MariaDB 10.3 and hibernate sequence generator feature.

Is the GlobalIdentifierGeneratorScopeTest.java intended for testing PostgreSQL and hibernate sequence generator?

Thanks
Ming Qin

Issue with serialization

There is a method
static T clone(T value)
in class JacksonUtil
which try to determine what type of deep copy use and in case if input Object implements Serializable interface it try to use java Serialization mechanism to copy object. But what if i use ArrayList to store it to DB and MyObject is not implements it. Why just for all cases not use json serialization?
Can you do it preferable may be. Because it always fails with exception and force me to mark my Object as Serializable. But it is not what i want

Problem with CompositeId and Bidirectional @OneToMany

Hi @vladmihalcea,

The below is a modified version of CompositeIdManyToOneTest to produce the problem I am running into. The problem is that there are duplicated attributes in the select statement issued by Hibernate.

select employees0_.company_id as company_2_1_0_, employees0_.employee_number as employee1_1_0_, employees0.company_id as company_2_1_1_, employees0_.employee_number as employee1_1_1__
from employee employees0_ where employees0_.company_id=?

public class CompositeIdOneToManyTest extends AbstractTest {
    @Override
    protected Class<?>[] entities() {
        return new Class<?>[] {
                Company.class,
                Employee.class
        };
    }

    @Test
    public void test() {
        doInJPA(entityManager -> {
            Company company = new Company();
            company.setId(1L);
            company.setName("CompanyA");

            Employee employee = new Employee();
            employee.setId(new EmployeeId(company, 1L));
            company.setEmployees(Collections.singletonList(employee));

            entityManager.persist(company);
        });

        doInJPA(entityManager -> {
            Company company = entityManager.find(Company.class, 1L);
            company.getEmployees().get(0).getId().getEmployeeNumber();
        });
    }


    @Entity(name = "Company")
    @Table(name = "company")
    public static class Company {

        @Id
        private Long id;

        private String name;

        @OneToMany(
            mappedBy="id.company",
            fetch=FetchType.LAZY,
            cascade=CascadeType.ALL
        )
        private List<Employee> employees;

        public Long getId() {
            return id;
        }

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

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public List<Employee> getEmployees() {
            return employees;
        }

        public void setEmployees(List<Employee> employees) {
            this.employees = employees;
        }
    }

    @Entity(name = "Employee")
    @Table(name = "employee")
    public static class Employee {

        @EmbeddedId
        private EmployeeId id;

        public EmployeeId getId() {
            return id;
        }

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

    @Embeddable
    public static class EmployeeId implements Serializable {

        @ManyToOne
        @JoinColumn(name = "company_id",insertable = false, updatable = false)
        private Company company;

        @Column(name = "employee_number")
        private Long employeeNumber;

        public EmployeeId() {
        }

        public EmployeeId(Company company, Long employeeId) {
            this.company = company;
            this.employeeNumber = employeeId;
        }

        public Company getCompany() {
            return company;
        }

        public Long getEmployeeNumber() {
            return employeeNumber;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (!(o instanceof EmployeeId)) return false;
            EmployeeId that = (EmployeeId) o;
            return Objects.equals(getCompany(), that.getCompany()) &&
                    Objects.equals(getEmployeeNumber(), that.getEmployeeNumber());
        }

        @Override
        public int hashCode() {
            return Objects.hash(getCompany(), getEmployeeNumber());
        }
    }
}

To be honest, I am clueless about the problem. Many thanks for your explanation.

Merry Christmas ๐ŸŽ„

The ebook from leanpub.com is impossible to buy from Russia

This is probably not the best place to describe an issue, but I haven't found a better one.

The ebook is only available via leanpub.com. This site uses only PayPal to process payments, and it is impossible to pay via PayPal from Russia:

http://help.leanpub.com/reader-help/im-having-a-problem-using-a-credit-card-from-russian-federation-help

https://groups.google.com/forum/#!topic/leanpub/EMVVGa9D6C4

Would you please consider additionally selling an electronic version of the book via another online store? I know that a print version is available on Amazon, but I really do not want to buy the printed one.

Compile not working / Oracle JAR requires authorization

When I do

mvn clean test-compile

There is the error:

[ERROR] Failed to execute goal on project high-performance-java-persistence: Could not resolve dependencies for project com.vladmihalcea.book:high-performance-java-persistence:pom:1.0-SNAPSHOT: Failed to collect dependencies at com.oracle.jdbc:ojdbc8:jar:12.2.0.1: Failed to read artifact descriptor for com.oracle.jdbc:ojdbc8:jar:12.2.0.1: Could not transfer artifact com.oracle.jdbc:ojdbc8:pom:12.2.0.1 from/to maven.oracle.com (https://maven.oracle.com): Not authorized , ReasonPhrase:Authorization Required. -> [Help 1]

When removing the Oracle dependency compilation works.

Probably the Oracle JDBC cannot be obtained from Maven central. One solution would be to put the Oracle dependency in a separate Maven profile.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.