Hibernate 5 入门指南-基于类注解
Posted 河图书卦
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Hibernate 5 入门指南-基于类注解相关的知识,希望对你有一定的参考价值。
-
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.url">jdbc:mysql://localhost:3306/databaseName?useSSL=false&serverTimezone=UTC&verifyServerCertifate=false&allowPublicKeyRetrieval=true</property>
<property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property>
<property name="connection.username">root</property>
<property name="connection.password">passwd</property>
?
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQL8Dialect</property>
?
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<property name="format_sql">true</property>
?
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create</property>
?
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
?
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.internal.NoCachingRegionFactory</property>
</session-factory>
</hibernate-configuration> -
创建实体Java类
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
?
import org.hibernate.annotations.GenericGenerator;
? -
向hibernate.cfg.xml文件中添加映射信息
<mapping class="类路径.Event"/>
-
JUnit测试测试程序
import java.util.Date;
import java.util.List;
?
import junit.framework.TestCase;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
?
public class AnnotationsIllustrationTest extends TestCase {
private SessionFactory sessionFactory;
?
-
运行测试
Hibernate: 十一月 01, 2018 11:54:51 上午 org.hibernate.resource.transaction.backend.jdbc.internal.DdlTransactionIsolatorNonJtaImpl getIsolatedConnection
?
INFO: HHH10001501: Connection obtained from JdbcConnectionAccess [org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess
以上是关于Hibernate 5 入门指南-基于类注解的主要内容,如果未能解决你的问题,请参考以下文章
《Java从入门到放弃》入门篇:使用注解的方式配置hibernate映射关系