Created
December 25, 2018 08:32
-
-
Save idrissov/e58bde28c0dd5d5dcf89b8c6c0667a03 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?xml version="1.0" encoding="UTF-8"?> | |
| <Configuration status="warn" name="MyApp" packages=""> | |
| <Appenders> | |
| <Console name="STDOUT" target="SYSTEM_OUT"> | |
| <PatternLayout pattern="%m%n"/> | |
| </Console> | |
| </Appenders> | |
| <Loggers> | |
| <Logger name="org.hibernate.SQL" level="debug" additivity="false"> | |
| <AppenderRef ref="STDOUT"/> | |
| </Logger> | |
| <Root level="debug"> | |
| <AppenderRef ref="STDOUT"/> | |
| </Root> | |
| </Loggers> | |
| </Configuration> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.github.idrissov.hibernate; | |
| import javax.persistence.Entity; | |
| import javax.persistence.GeneratedValue; | |
| import javax.persistence.GenerationType; | |
| import javax.persistence.Id; | |
| @Entity | |
| public class MyJpaClass { | |
| @Id | |
| @GeneratedValue (strategy= GenerationType.IDENTITY) | |
| private Long id; | |
| private String message; | |
| public Long getId() { | |
| return id; | |
| } | |
| public void setId(Long id) { | |
| this.id = id; | |
| } | |
| public String getMessage() { | |
| return message; | |
| } | |
| public void setMessage(String message) { | |
| this.message = message; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?xml version="1.0" encoding="UTF-8"?> | |
| <persistence xmlns="http://java.sun.com/xml/ns/persistence" | |
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
| xsi:schemaLocation="http://java.sun.com/xml/ns/persistence | |
| http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" | |
| version="2.0"> | |
| <persistence-unit name="HelloWorldPU" transaction-type="RESOURCE_LOCAL"> | |
| <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider> | |
| <properties> | |
| <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/> | |
| <property name="javax.persistence.jdbc.driver" value="org.h2.Driver"/> | |
| <property name="javax.persistence.jdbc.url" value="jdbc:h2:mem:test;MODE=MySQL;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE"/> | |
| <property name="javax.persistence.jdbc.user" value="sa"/> | |
| <property name="javax.persistence.jdbc.password" value=""/> | |
| <property name="hibernate.show_sql" value="true"/> | |
| <property name="hibernate.format_sql" value="true"/> | |
| <property name="hibernate.use_sql_comments" value="true"/> | |
| <property name="hibernate.transaction.factory_class" | |
| value="org.hibernate.engine.transaction.internal.jta.CMTTransactionFactory"/> | |
| <property name="hibernate.id.new_generator_mappings" value="true"/> | |
| <property name="hibernate.connection.autocommit" value="false"/> | |
| <property name="hibernate.hbm2ddl.auto" value="create-drop"/> | |
| </properties> | |
| </persistence-unit> | |
| </persistence> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.github.idrissov.hibernate; | |
| import static org.junit.Assert.assertNotNull; | |
| import java.io.FileNotFoundException; | |
| import java.sql.SQLException; | |
| import javax.persistence.EntityManager; | |
| import javax.persistence.EntityManagerFactory; | |
| import javax.persistence.Persistence; | |
| import org.apache.logging.log4j.LogManager; | |
| import org.apache.logging.log4j.Logger; | |
| import org.junit.AfterClass; | |
| import org.junit.BeforeClass; | |
| import org.junit.Test; | |
| public class PersistTest { | |
| Logger LOG = LogManager.getLogger(PersistTest.class); | |
| protected static EntityManagerFactory emf; | |
| protected static EntityManager em; | |
| @BeforeClass | |
| public static void init() throws FileNotFoundException, SQLException { | |
| emf = Persistence.createEntityManagerFactory("HelloWorldPU"); | |
| em = emf.createEntityManager(); | |
| } | |
| @AfterClass | |
| public static void tearDown(){ | |
| em.clear(); | |
| em.close(); | |
| emf.close(); | |
| } | |
| @Test | |
| public void testPersist_success() { | |
| em.getTransaction().begin(); | |
| MyJpaClass o = new MyJpaClass(); | |
| o.setMessage("text"); | |
| em.persist(o); | |
| em.getTransaction().commit(); | |
| assertNotNull(o.getId()); | |
| LOG.debug(o.getId()); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?xml version="1.0" encoding="UTF-8"?> | |
| <project xmlns="http://maven.apache.org/POM/4.0.0" | |
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
| xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
| <modelVersion>4.0.0</modelVersion> | |
| <groupId>com.github.idrissov</groupId> | |
| <artifactId>hibernate-sample</artifactId> | |
| <version>1.0-SNAPSHOT</version> | |
| <dependencyManagement> | |
| <dependencies> | |
| <dependency> | |
| <groupId>org.apache.logging.log4j</groupId> | |
| <artifactId>log4j-bom</artifactId> | |
| <version>2.11.1</version> | |
| <scope>import</scope> | |
| <type>pom</type> | |
| </dependency> | |
| </dependencies> | |
| </dependencyManagement> | |
| <dependencies> | |
| <!-- https://mvnrepository.com/artifact/com.h2database/h2 --> | |
| <dependency> | |
| <groupId>com.h2database</groupId> | |
| <artifactId>h2</artifactId> | |
| <version>1.4.197</version> | |
| <scope>test</scope> | |
| </dependency> | |
| <!-- https://mvnrepository.com/artifact/junit/junit --> | |
| <dependency> | |
| <groupId>junit</groupId> | |
| <artifactId>junit</artifactId> | |
| <version>4.12</version> | |
| <scope>test</scope> | |
| </dependency> | |
| <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core --> | |
| <dependency> | |
| <groupId>org.hibernate</groupId> | |
| <artifactId>hibernate-core</artifactId> | |
| <version>5.4.0.Final</version> | |
| </dependency> | |
| <dependency> | |
| <groupId>org.apache.logging.log4j</groupId> | |
| <artifactId>log4j-api</artifactId> | |
| </dependency> | |
| <dependency> | |
| <groupId>org.apache.logging.log4j</groupId> | |
| <artifactId>log4j-core</artifactId> | |
| </dependency> | |
| </dependencies> | |
| </project> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment