Java.Hibernate.SimpleExample

/resources/hibernate.properties

hibernate.connection.url = jdbc:mysql://localhost:3307/library
    public static void main(String[] args) {
        SessionFactory sessionFactory = new Configuration()
                .addAnnotatedClass(org.example.entity.AuthorEntity.class)
                .buildSessionFactory();

        try (Session session = sessionFactory.openSession()) {
         List<AuthorEntity> list = session.createQuery("from AuthorEntity", AuthorEntity.class).list();
            AuthorEntity authorEntity = list.get(0);
            System.out.println(authorEntity.getFirstName());

            var transaction = session.beginTransaction();
            authorEntity.setFirstName("Robert");
            session.save(authorEntity);
            transaction.commit();
        }
    }
Posted in Без рубрики | Comments Off on Java.Hibernate.SimpleExample

Java.Jdbc.SimpleExample

build.gradle.kts

dependencies {
    implementation("mysql:mysql-connector-java:8.0.33")
    testImplementation(platform("org.junit:junit-bom:5.10.0"))
    testImplementation("org.junit.jupiter:junit-jupiter")
}

Main.java

    public static void main(String[] args) throws SQLException {
        System.out.printf("Hello and welcome!");

public class Main {

    public static void main(String[] args) throws SQLException {
        System.out.printf("Hello and welcome!");

        try (Connection connection = DriverManager.getConnection(
                "jdbc:mysql://localhost:3307", "root", "root"
        )) {
            Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery("select * from library.author");
            while (resultSet.next()) {
                int id = resultSet.getInt(1);
                String name = resultSet.getString(2);
                System.out.printf("ID: %d, Name: %s\n", id, name);
            }

            jdbcMetaDataExample(resultSet);

        }
    }

    private static void jdbcMetaDataExample(ResultSet resultSet) throws SQLException {
        var metaData = resultSet.getMetaData();
        for (int i = 1; i < metaData.getColumnCount(); i++) {
            System.out.println(metaData.getColumnName(i));
        }
    }
}
Posted in Без рубрики | Comments Off on Java.Jdbc.SimpleExample

SQL. Calc the sum with case, example

select 
  sum(case town_to
    when 'LosAngeles' then 1 else 0
  end)
from trip;
Posted in Без рубрики | Comments Off on SQL. Calc the sum with case, example

Mullvad – free the internet :)

no more freedom downtime

https://mullvad.net/en

Posted in Без рубрики | Comments Off on Mullvad – free the internet :)

Junit. Jupiter

test order example

https://github.com/junit-team/junit4/wiki/aggregating-tests-in-suites

import org.junit.runner.RunWith;
import org.junit.runners.Suite;

@RunWith(Suite.class)
@Suite.SuiteClasses({
  TestFeatureLogin.class,
  TestFeatureLogout.class,
  TestFeatureNavigate.class,
  TestFeatureUpdate.class
})

public class FeatureTestSuite {
  // the class remains empty,
  // used only as a holder for the above annotations
}
Posted in Без рубрики | Comments Off on Junit. Jupiter

Java. SpringBoot Example how to work with dateTime in Specification

Working example

    public static <T, V> Specification<T> findByWithTypeCasting5(String functionName, String paramName, V param, Class<T> entityType) {

        checkParam(paramName, entityType);

        return ((root, query, criteriaBuilder) -> param == null ? criteriaBuilder.conjunction()
                : criteriaBuilder.equal(criteriaBuilder.function(
                        "TO_CHAR",
                        String.class,
                        root.get(paramName),
                        criteriaBuilder.literal("YYYY-MM-DD HH24:MI:SS.000")
                ),
                param
        ));

    }

just with date

        return ((root, query, criteriaBuilder) -> param == null ? criteriaBuilder.conjunction()
                : criteriaBuilder.equal(
                root.<Date>get(paramName),

                param
        ));

or like that

    public static <T, V> Specification<T> findByWithTypeCasting2(String functionName, String paramName, V param, Class<T> entityType) {

        checkParam(paramName, entityType);

        return ((root, query, criteriaBuilder) -> param == null ? criteriaBuilder.conjunction()
                : criteriaBuilder.equal(criteriaBuilder.function("date", LocalDate.class, root.get(paramName)), param));
    }
Posted in Без рубрики | Comments Off on Java. SpringBoot Example how to work with dateTime in Specification

Java.SpringBoot.PopularAnnotations

@BeanAnnotations

@EnableAsync / @Async ; do smth in new thread

@EnableSheduling / @Scheduled

@Lock ; lock record on database level

@Mdifying ; to learn

@Scheduled

@Query

Posted in Без рубрики | Comments Off on Java.SpringBoot.PopularAnnotations

SpringBoot. Exception Management

in rus

https://struchkov.dev/blog/ru/exception-handling-controlleradvice/

validation handling

https://struchkov.dev/blog/ru/spring-boot-validation/

Posted in Без рубрики | Comments Off on SpringBoot. Exception Management

Java.Hibernate.JoinTableAnnotation

https://stackoverflow.com/questions/5478328/in-which-case-do-you-use-the-jpa-jointable-annotation

Posted in Без рубрики | Comments Off on Java.Hibernate.JoinTableAnnotation

SpringBoot.Making our first starter and autoconfiguration

STARTER

let’s create some new module and infra package in it

package infra;

public class SomeInfraBean {
    public void sayHello() {
        System.out.println("hello from infra bean");
    }
}
package infra;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class SomeInfraConfiguration {
    @Bean
    public SomeInfraBean someInfraBean() {
        return new SomeInfraBean();
    }
}
plugins {
    id("java")
    id("org.springframework.boot") version "3.2.4"
    id("io.spring.dependency-management") version "1.1.4"
}

group = "org.example"
version = "unspecified"

repositories {
    mavenCentral()
}

dependencies {
    implementation("org.springframework.boot:spring-boot-starter")
    compileOnly("org.projectlombok:lombok")
    annotationProcessor("org.projectlombok:lombok")
    testImplementation(platform("org.junit:junit-bom:5.9.1"))
    testImplementation("org.junit.jupiter:junit-jupiter")
}

tasks.test {
    useJUnitPlatform()
}

//val jar: Jar by tasks
//val bootJar: BootJar by tasks
//
//bootJar.enabled = false
//jar.enabled = true
//
//
//tasks.getByName<BootJar>("bootJar") {
//    enabled = false
//}
//
//tasks.getByName<Jar>("jar") {
//    enabled = true
//}

MAIN APP

in main app we can call and use this bean like this

import infra.SomeInfraBean;
import jakarta.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
public class SomeService {

    @Autowired
    private SomeInfraBean someInfraBean;

    @PostConstruct
    public void testInfrabean() {
        someInfraBean.sayHello();
    }


    @Value("${mymessage}")
    private void init(String msg) {
        System.out.println(msg);
    }
}

Posted in Без рубрики | Comments Off on SpringBoot.Making our first starter and autoconfiguration