Last active
October 22, 2016 23:57
-
-
Save thergbway/ac86fb9aa97b00bf70104b1534721e36 to your computer and use it in GitHub Desktop.
HIBERNATE&JPA. JPA HIBERNATE CONFIGURATION
This file contains 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
См. сводную информацию в https://gist.github.com/thergbway/0a68550a27668ce571e7c72cf6e6078f |
This file contains 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 intro; | |
import lombok.AllArgsConstructor; | |
import lombok.Data; | |
import lombok.NoArgsConstructor; | |
import org.hibernate.annotations.GenericGenerator; | |
import javax.persistence.*; | |
import java.util.Date; | |
@Data | |
@NoArgsConstructor | |
@AllArgsConstructor | |
@Entity | |
@Table(name = "CARS") | |
public class Car { | |
@Id | |
@GeneratedValue(generator = "increment") | |
@GenericGenerator(name = "increment", strategy = "increment") | |
private Long id; | |
private String title; | |
@Temporal(TemporalType.TIMESTAMP) | |
@Column(name = "CAR_DATE") | |
private Date date; | |
} |
This file contains 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"?> | |
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"> | |
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"> | |
<appender name="stdout" class="org.apache.log4j.ConsoleAppender"> | |
<param name="Target" value="System.out"/> | |
<layout class="org.apache.log4j.PatternLayout"> | |
<param name="ConversionPattern" value="%d{ABSOLUTE} %5p %c{1}:%L - %m%n"/> | |
</layout> | |
</appender> | |
<!--Turn on your favourite loggers--> | |
<logger name="org.hibernate"> | |
<level value="INFO"/><!--or DEBUG for more verbose logging--> | |
</logger> | |
<logger name="org.hibernate.SQL"> | |
<level value="INFO"/> | |
</logger> | |
<root> | |
<level value="WARN"/> | |
<appender-ref ref="stdout"/> | |
</root> | |
</log4j:configuration> |
This file contains 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 intro; | |
import org.apache.log4j.Logger; | |
import javax.persistence.EntityManager; | |
import javax.persistence.EntityManagerFactory; | |
import javax.persistence.EntityTransaction; | |
import javax.persistence.Persistence; | |
import java.util.Date; | |
import java.util.Objects; | |
public class Main { | |
private static final Logger log = Logger.getLogger(Main.class); | |
public static void main(String[] args) { | |
EntityManagerFactory entityManagerFactory = null; | |
EntityManager entityManager = null; | |
EntityTransaction tx = null; | |
try { | |
entityManagerFactory = Persistence.createEntityManagerFactory("hibernate-jpa-persistence-unit"); | |
entityManager = entityManagerFactory.createEntityManager(); | |
tx = entityManager.getTransaction(); | |
tx.begin(); | |
entityManager.persist(new Car(null, "Our very first event!", new Date())); | |
entityManager.persist(new Car(null, "A follow up event", new Date())); | |
tx.commit(); | |
entityManager.close(); | |
entityManager = entityManagerFactory.createEntityManager(); | |
tx = entityManager.getTransaction(); | |
tx.begin(); | |
entityManager.createQuery("from Car", Car.class).getResultList().forEach(System.out::println); | |
tx.commit(); | |
} catch (Exception e) { | |
log.error("Got error!", e); | |
if (Objects.nonNull(tx) && tx.isActive()) | |
tx.rollback(); | |
} finally { | |
if (Objects.nonNull(entityManager)) | |
entityManager.close(); | |
if (Objects.nonNull(entityManagerFactory)) | |
entityManagerFactory.close(); | |
} | |
} | |
} |
This file contains 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
<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="hibernate-jpa-persistence-unit"> | |
<description>Persistence unit for the Hibernate JPA</description> | |
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider> | |
<class>intro.Car</class> | |
<properties> | |
<property name="javax.persistence.jdbc.driver" value="org.h2.Driver"/> | |
<property name="javax.persistence.jdbc.url" value="jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1;MVCC=TRUE"/> | |
<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.hbm2ddl.auto" value="create"/> | |
</properties> | |
</persistence-unit> | |
</persistence> |
This file contains 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>org.thergbway</groupId> | |
<artifactId>hibernate_jpa_examples</artifactId> | |
<version>1.0-SNAPSHOT</version> | |
<properties> | |
<maven.compiler.source>1.8</maven.compiler.source> | |
<maven.compiler.target>1.8</maven.compiler.target> | |
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | |
</properties> | |
<dependencies> | |
<dependency> | |
<groupId>com.h2database</groupId> | |
<artifactId>h2</artifactId> | |
<version>1.4.192</version> | |
</dependency> | |
<dependency> | |
<groupId>postgresql</groupId> | |
<artifactId>postgresql</artifactId> | |
<version>9.1-901-1.jdbc4</version> | |
</dependency> | |
<dependency> | |
<groupId>junit</groupId> | |
<artifactId>junit</artifactId> | |
<version>4.12</version> | |
</dependency> | |
<dependency> | |
<groupId>org.slf4j</groupId> | |
<artifactId>slf4j-log4j12</artifactId> | |
<version>1.7.21</version> | |
</dependency> | |
<dependency> | |
<groupId>com.mchange</groupId> | |
<artifactId>c3p0</artifactId> | |
<version>0.9.5.2</version> | |
</dependency> | |
<dependency> | |
<groupId>org.javassist</groupId> | |
<artifactId>javassist</artifactId> | |
<version>3.20.0-GA</version> | |
</dependency> | |
<dependency> | |
<groupId>org.hibernate</groupId> | |
<artifactId>hibernate-entitymanager</artifactId> | |
<version>5.2.3.Final</version> | |
</dependency> | |
<dependency> | |
<groupId>org.hibernate</groupId> | |
<artifactId>hibernate-ehcache</artifactId> | |
<version>5.2.3.Final</version> | |
</dependency> | |
<dependency> | |
<groupId>org.hibernate</groupId> | |
<artifactId>hibernate-validator</artifactId> | |
<version>5.2.4.Final</version> | |
</dependency> | |
<dependency> | |
<groupId>net.sf.ehcache</groupId> | |
<artifactId>ehcache-core</artifactId> | |
<version>2.6.11</version> | |
</dependency> | |
<dependency> | |
<groupId>org.projectlombok</groupId> | |
<artifactId>lombok</artifactId> | |
<version>1.16.10</version> | |
</dependency> | |
</dependencies> | |
<build> | |
<plugins> | |
<plugin> | |
<groupId>org.apache.maven.plugins</groupId> | |
<artifactId>maven-clean-plugin</artifactId> | |
<version>3.0.0</version> | |
<executions> | |
<execution> | |
<id>auto-clean</id> | |
<phase>initialize</phase> | |
<goals> | |
<goal>clean</goal> | |
</goals> | |
</execution> | |
</executions> | |
</plugin> | |
<plugin> | |
<groupId>org.apache.maven.plugins</groupId> | |
<artifactId>maven-compiler-plugin</artifactId> | |
<version>3.5.1</version> | |
<configuration> | |
<source>${maven.compiler.source}</source> | |
<target>${maven.compiler.target}</target> | |
</configuration> | |
<executions> | |
<execution> | |
<goals> | |
<goal>compile</goal> | |
</goals> | |
</execution> | |
</executions> | |
</plugin> | |
</plugins> | |
</build> | |
</project> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment