View

implementation 'com.h2database:h2:1.4.200'
public class App {
  public static void main(String[] args) throws Exception {

    final String[] H2_ARGS = new String[]{
      "-web"
      , "-webPort", "9091"
      , "-webAdminPassword", ""
      , "-browser"
      , "-tcp"
      , "-tcpAllowOthers"
      , "-tcpPort", "9090"
      , "-key", "test", "test"
    };


    Server h2TcpServer = Server.createTcpServer(H2_ARGS);
    Server h2WebServer = Server.createWebServer(H2_ARGS);
    h2TcpServer.start();
    h2WebServer.start();
    System.out.println(h2TcpServer.getStatus());
    System.out.println(h2WebServer.getStatus());
  }
}

Create EntityManager

implementation 'org.hibernate:hibernate-entitymanager:5.6.4.Final'
Class.forName("org.h2.Driver");
Connection con = DriverManager.getConnection(
"jdbc:h2:mem:tcp://localhost:9090/test", "sa", "");

EntityManagerFactory emf =
Persistence.createEntityManagerFactory("test_persistence_unit");
EntityManager entityManager = emf.createEntityManager();
// /main/resources/META-INF/persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             version="2.2" xmlns="http://xmlns.jcp.org/xml/ns/persistence"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_2.xsd">
  <persistence-unit name="test_persistence_unit">
    <properties>
      <property name="javax.persistence.jdbc.driver" value="org.h2.Driver"/>
      <property name="javax.persistence.jdbc.user" value="sa"/>
      <property name="javax.persistence.jdbc.password" value=""/>
      <property name="javax.persistence.jdbc.url" value="jdbc:h2:mem:tcp://localhost:9090/test"/>
      <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>

      <property name="hibernate.show_sql" value="true"/>
      <property name="hibernate.format_sql" value="true"/>
      <property name="hibernate.use_sql_comments" value="true"/>
    </properties>
  </persistence-unit>
</persistence>

'Program language > Java' 카테고리의 다른 글

디자인 패턴 cheat sheet  (0) 2022.04.10
Java Reflection  (0) 2018.04.03
Share Link
reply