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();



Bean Validation @GroupSequence Anotasyonu

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