20 Şubat 2022 Pazar

persistence.xml

Giriş
src/main/resources/META-INF/persistence.xml dizinindedir
Örnek
Şöyle yaparız. jta-data-source ile JNDI ismi veriliyor.
<?xml version="1.0" encoding="UTF-8"?>
  <persistence version="2.1"
    xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd ">
  <persistence-unit name="MyAppPU" transaction-type="JTA">
    <jta-data-source>java:app/jdbc/MyApp</jta-data-source>
    <shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
    <properties>
      <!-- JBoss Wildfly's Hibernate 4 specific JPA properties -->
      <property name="hibernate.cache.use_second_level_cache" value="true" />
      <property name="hibernate.hbm2ddl.auto" value="validate" /><!-- NEVER: update -->
      <property name="hibernate.show_sql" value="false" />
      <property name="hibernate.format_sql" value="false"/>
      <property name="hibernate.use_sql_comments" value="false" />
      <property name="hibernate.enable_lazy_load_no_trans" value="true"/>
      <!--  For Performance monitoring on Hibernate -->
      <property name="hibernate.generate_statistics" value="false"/>
      <property name="hibernate.cache.use_structured_entries" value="false"/>
   
      <!-- TomEE PluME 1.7.2 and higher with EclipseLink  -->
      <property name="eclipselink.logging.logger" value="JavaLogger" />
      </properties>
  </persistence-unit>
</persistence>
Kullanmak için şöyle yaparız
@PersistenceContext(unitName="MyAppPU")
private EntityManager entityManager;


Servlet web.xml data-source Tag

Örnek
Şöyle yaparız
<data-source>
  <name>java:global/ExampleDataSource</name>
  <class-name>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</class-name>
  <server-name>localhost</server-name>
  <port-number>3306</port-number>
  <database-name>mysql</database-name>
  <user>root</user>
  <password>root</password>
  <!-- Example of how to use a Payara specific custom connection pool setting -->
  <property>
     <name>fish.payara.sql-trace-listeners</name>
     <value>com.sun.gjc.util.SQLTraceLogger</value>
  </property>
</data-source>
Oracle için şöyle yaparız
<class-name>oracle.jdbc.pool.OracleDataSource</class-name>
PostgreSQL için şöyle yaparız
<class-name>org.postgresql.ds.PGSimpleDataSource</class-name>
Ortam değişkenlerini kullanmak için şöyle yaparız
<server-name>${server.name}</server-name>
Bu kaynağı kullanmak için şöyle yaparız
@Resource(name=”java:global/ExampleDataSource”)
payara-resource.xml
src/main/java/webapp/WEB-INF dizinindedir.
Örnek
Şöyle yaparız
<resources>
  <jdbc-resource pool-name="MyAppDS"
                jndi-name="java:app/jdbc/MyApp"
                enable="true"/>
  <jdbc-connection-pool name="MySQLPool"
           res-type="javax.sql.XADataSource"
           datasource-classname="com.mysql.cj.jdbc.MysqlXADataSource">
    <property name="url" value="jdbc:h2:mem:hibernateExample"/>
    <property name="User" value="testUser"></property>
    <property name="Password" value="testPassword"></property>
    <property name="DatabaseName" value="myapp_db"></property>
    <property name="ServerName" value="localhost"></property>
    <property name="PortNumber" value="3306"></property>
  </jdbc-connection-pool>
</resources>
Şöyle yaparız
@PersistenceContext(unitName="MyAppPU")
private EntityManager entityManager;



7 Şubat 2022 Pazartesi

Jakarta EE CDI Scope Anotasyonları

Giriş
Açıklaması şöyle
The default scope in CDI is the @Dependent scope, which means that the bean receives the same lifecycle as the bean that it is injected into.

CDI also provides the following annotations for further built-in scopes:
Java

@ApplicationScoped
@RequestScoped
@SessionScoped
@ConversationScoped
@Singleton
CDI vs EJB Scopes
Açıklaması şöyle
Additional, non-CDI lifecycles are defined by the Jakarta Enterprise Beans (formerly known as EJB) specification, most notably
@Singleton: A singleton EJB’s lifecycle is similar to that of an application-scoped bean.
@Stateless: Stateless EJBs are kept in internal pool by the application server, which enables efficient re-use of EJBs. Moreover, the server guarantees that only one thread executes a stateless EJB at any time, meaning that they are relatively thread-safe (unless, for instance, they modify static variables).

The most important difference between Jakarta Enterprise Beans and CDI beans is that EJBs provide additional features such as transaction management and concurrency management.

And finally, there is also the @Singleton scope with the same name as the EJB scope mentioned above, but from another package (javax.inject.Singleton instead of javax.ejb.Singleton). In contrast to @ApplicationScoped and javax.ejb.Singleton, these beans are not injected with a proxy.
CDI: Bean Definitions
Açıklaması şöyle
Classes marked with the following bean-defining annotations are picked up for dependency injection:
@Dependent
 
@NormalScope
  @ApplicationScoped
  @RequestScoped
  @SessionScoped
  @ConversationScoped

@Stereotype
Açıklaması şöyle
Note that normal-scoped annotations are sufficient for a bean to become injectable in CDI, which is not the case in Spring (where you have to use annotations such as @Component).
Açıklaması şöyle
Jakarta Enterprise Beans are also eligible for injection (most notably: @Stateless and @Singleton).

Moreover, injectable beans can be defined by annotating a method with @Produces, and returning the desired bean from this method:

@Produces
MyBean mb() {
  return new MyBean();
}

For simpler use cases, you can also annotate a field instead:
@Produces
MyBean mb = new MyBean();



30 Ocak 2022 Pazar

Jakarta EE JPA

Giriş
Şeklen şöyle. Şu anda JPA 3.1 son sürüm.

Maven
Örnek - Eski
Şu satırı dahil ederiz
<dependency>
  <groupId>javax.persistence</groupId>
  <artifactId>javax.persistence-api</artifactId>
  <version>${javax.persistence.version}</version>
</dependency>
Örnek
Şu satırı dahil ederiz
<dependency>
<groupId>jakarta.persistence</groupId> <artifactId>jakarta.persistence-api</artifactId> <version>3.1.0</version> // latest version </dependency>
Örnek - Hibernate
Şu satırı dahil ederiz. Burada JPA sağlayıcısı olarak Hibernate kullanılıyor
<dependency>
  <groupId>jakarta.persistence</groupId>
  <artifactId>jakarta.persistence-api</artifactId>
  <version>3.0.0</version>
</dependency>
<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-core-jakarta</artifactId>
  <version>5.6.4.Final</version>
</dependency>
<dependency>
  <groupId>org.glassfish.jaxb</groupId>
  <artifactId>jaxb-runtime</artifactId>
  <version>3.0.0</version>
</dependency>

20 Ocak 2022 Perşembe

JAX-RS @ApplicationPath Anotasyonu - web.xml yerine geçer

Giriş
Şu satırı dahil ederiz
import jakarta.ws.rs.ApplicationPath;
Örnek
Şöyle yaparız. /api/* isteklerini karşılayan bir servlet başlatır
import jakarta.ws.rs.ApplicationPath;
import jakarta.ws.rs.core.Application; @ApplicationPath("api") public class JerseyConfigs extends Application { } // Access as /api/hello import jakarta.ws.rs.GET; import jakarta.ws.rs.Path; @Path("hello") public class HelloController { @GET public String getHello() { return "Hello, JAX-RS !!!"; } }
Açıklaması şöyle
By simply extending the Application class from JAX-RS, and adding the @ApplicationPath annotation, now you’ve configured Jersey.

Through the annotation you can define the url pattern, and other Jersey configurations can be added by overriding methods in the Application class.

Note we didn’t specify packages to scan, what will happen is that jersey will scan all our application classes for JAX-RS annotated classes.
Örnek
Şöyle yaparız/api/* isteklerini karşılayan bir servlet başlatır
import jakarta.ws.rs.ApplicationPath;
import jakarta.ws.rs.core.Application;

@ApplicationPath("api")
public class BaseApplication extends Application {

}

Jakarta Kullanımı

Neden Paket İsimleri Değişti?
Açıklaması şöyle. Eclipse Foundation teknolojyi Jakarta EE olarak yeniden markalaştırdı ama kod paketlerinin değişmesinin ana sebebi Oracle'ın javax paket ismini kullandırmak istememesi.
After Oracle turned the Java EE specs over to the Eclipse Foundation, they have since rebranded it as Jakarta EE. This includes servlets, JMS, JPA, and many others. What was once Java Messaging Service is now Jakarta Messaging Service.

And Jakarta EE 9 is the version where all these specs changes their package prefix from javax to jakarta. (Oracle wouldn’t grant Eclipse the javax prefix nor give them any sort of license).
Paket İsimlerini Değiştirmek
Eclipse Transformer kullanarak şöyle yaparız
java -jar org.eclipse.transformer.cli-0.4.0-SNAPSHOT-all.jar \ -i input_directory \ -o output_directory \ -x jakarta-renames.properties \ -t jakarta-direct.properties
1. Jakarta EE 8
Eclipse Foundation tarafından verilen ilk sürüm. Bu sürüm için iki tane dependency var. 

Birinci Kullanım
Açıklaması şöyle. Yani sadece bağımlılığı değiştirmek yeterli. Kodda paket isimler değişmiyor.
Jakarta EE8 was the first release after the Eclipse foundation overtook the project from Oracle. Nothing but the name has changed. Your existing Java EE8 projects will work out of the box. In your Maven dependencies, you can now use the new Jakarta EE8 platform reference.
Maven
Jakarta EE 8 için şu satırı dahil ederiz
<dependency>
<groupId>javax</groupId> <artifactId>javaee-api</artifactId> <version>8.0.1</version> <scope>provided</scope> </dependency>
Gradle
Şu satırı dahil ederiz
compileOnly 'javax:javaee-api:8.0.1'
İkinci Kullanım
Bu yeni bağımlılık ile kod içindeki paket isimleri artık javax.* yerine jakarta.* haline geliyor. Şöyle yaparız
<dependency>
<groupId>jakarta.platform</groupId> <artifactId>jakarta.jakartaee-api</artifactId> <version>8.0.0</version> <scope>provided</scope> </dependency>
2. Jakarta EE 9
Açıklaması şöyle. Yani kodda sadece namespace değişikliği gerekiyor
The goal of Jakarta EE 9 was the namespace change from javax.* to jakarta.*.
...
... Jakarta EE9 brings no special changes. Since the namespace change behind the scenes is very complex, this release was necessary for platform and library developers.  
Açıklaması şöyle. Benzer bir açıklama burada
By the way, most of the application servers will do the namespace migration for you during deployment. So as an application developer you can still use the javax.* namespaces, even if your target runtime will be Jakarta EE9. This means your ‘old’ application using the javax.* namespace will be running fine on newer Jakarta EE severs. 
Yani elimizde şöyle eski bir kod olsun. Bu kod sorunsuz çalışır
/* Jakarta EE8 */
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

@ApplicationPath("api")
public class BaseApplication extends Application {

}
Yani elimizde şöyle yeni bir kod olsun. Bu kod sorunsuz da çalışır
/* Jakarta EE9 */
import jakarta.ws.rs.ApplicationPath; import jakarta.ws.rs.core.Application; @ApplicationPath("api") public class BaseApplication extends Application { }
Eski kodlar Wildfly 26'ya kadar sorunsuz çalışacak. Açıklaması şöyle. Daha sonra her şeyin tamamen yeni isim alanına geçmesi gerekiyor. 
Applications using the jakarta.* namespace will only work on fully compatible Jakarta EE9 servers (e.g., wildfly-preview-26.0.0). Most of the official Jakarta EE servers are still on Jakarta EE 8. So from the perspective of an application developer, it currently makes no sense to switch to the new namespace. 

3. Jakarta EE 9.1
Açıklaması şöyle. Java 11 desteği geliyor
The Jakarta EE9.1 project is similar to Jakarta EE9 just an intermediate release. The difference between Jakarta EE 9 and 9.1 is the JDK 11 support.
Maven
Şu satırı dahil ederiz
<dependency>
  <groupId>jakarta.platform</groupId>
  <artifactId>jakarta.jakartaee-api</artifactId>
  <version>9.1.0</version>
  <scope>provided</scope>
</dependency>
Gradle
Şu satırı dahil ederiz
dependencies { providedCompile 'jakarta.platform:jakarta.jakartaee-api:9.1.0' }
4. Jakarta EE 10
Açıklaması şöyle
Jakarta Servlet (6.0)
Jakarta Server Pages (3.1)
Jakarta Expression Language (5.0)
Jakarta WebSocket (2.1)
Jakarta Authentication (3.0)
JAX-RS 3.1

Jakarta EE @Decorator Anotasyonu

Örnek
Şöyle yaparız
import javax.annotation.Priority;
import javax.decorator.Decorator;
import javax.decorator.Delegate;
import javax.enterprise.inject.Any;
import javax.inject.Inject;
import javax.interceptor.Interceptor;

@Decorator
@Priority(Interceptor.Priority.APPLICATION)
public class Manager implements Worker {

  @Inject
  @Delegate
  @Any
  private Worker worker;

  @Override
  public String work(String job) {
    return "will delegate to a programmer -> " + worker.work(job);
  }
}


Bean Validation @GroupSequence Anotasyonu

Örnek Elimizde şöyle bir kod olsun public class SampleRequest {   @NotNull   LocalDate startDate;   @NotNull   LocalDate endDate;   @AssertT...