标签:
之前开发时我用的是配置文件的方式,实现实体类和数据库中数据的映射关系,后来看到也有注解的方式,也比较方便,就想试试,去Hibernate官网下载了最新版的Hibernate5.1.final版本,直接搭建好环境,进行测试,结果遇到了好几个问题,抛出好几个异常信息。不过逐渐的被解决了。网上对这几个异常处理的解决方案比较少,这里分享一下我是怎么解决的。源码地址:点击打开链接
INFO: HHH000423: Disabling contextual LOB creation as JDBC driver reported JDBC version [3] less than 4 java.lang.NoSuchMethodError: javax.persistence.Table.indexes()[Ljavax/persistence/Index; at org.hibernate.cfg.annotations.EntityBinder.processComplementaryTableDefinitions (EntityBinder.java:1087) at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:767) at org.hibernate.boot.model.source.internal.annotations.AnnotationMetadataSourceProcessorImpl.processEntity Hierarchies(AnnotationMetadataSourceProcessorImpl.java:245) at org.hibernate.boot.model.process.spi.MetadataBuildingProcess$1.processEntityHierarchies (MetadataBuildingProcess.java:222) at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete (MetadataBuildingProcess.java:265) at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.build (MetadataBuildingProcess.java:83) at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:418) at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:87) at org.hibernate.boot.MetadataSources.buildMetadata(MetadataSources.java:179) at com.siti.test.HiberTest.<clinit>(HiberTest.java:26)
@Entity @Table(name = "user") public class User { private Long userId; private String userName; private String password; public User() { super(); } public User(Long userId, String userName, String password) { super(); this.userId = userId; this.userName = userName; this.password = password; } @Id @GeneratedValue(generator="increment") @GenericGenerator(name="increment", strategy = "increment") public Long getUserId() { return userId; }
<?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.connection.driver_class"> com.mysql.jdbc.Driver </property> <property name="hibernate.connection.url"> jdbc:mysql://localhost:3306/hiber </property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">root</property> <property name="dialect"> org.hibernate.dialect.MySQLDialect </property> <property name="connection.pool_size">1</property> <property name="show_sql">true</property> <property name="hibernate.hbm2ddl.auto">update</property> <mapping class="com.siti.domain.User"/> </session-factory> </hibernate-configuration>
package com.siti.domain; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Table; import org.hibernate.annotations.GenericGenerator; @Entity(name = "user") public class User { private Long userId; private String userName; private String password; public User() { super(); } public User(Long userId, String userName, String password) { super(); this.userId = userId; this.userName = userName; this.password = password; } @Id @GeneratedValue(generator="increment") @GenericGenerator(name="increment", strategy = "increment") public Long getUserId() { return userId; } public void setUserId(Long userId) { this.userId = userId; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return "User [userId=" + userId + ", userName=" + userName + ", password=" + password + "]"; } }测试代码:
package com.siti.test; import java.util.List; 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; import org.hibernate.criterion.Projections; import org.hibernate.criterion.Restrictions; import com.siti.domain.User; public class HiberTest { static SessionFactory sessionFactory = null; static { final StandardServiceRegistry registry = new StandardServiceRegistryBuilder() .configure() // configures settings from hibernate.cfg.xml .build(); sessionFactory = new MetadataSources( registry ).buildMetadata().buildSessionFactory(); /*sessionFactory = new Configuration() .configure() .buildSessionFactory();*/ } public static void main(String[] args) { testInsertUser(); } /** * 1. 添加用户 * Hibernate: insert into user (userName, password) values (?, ?) */ public static void testInsertUser(){ User user = new User(); user.setUserId(5L); user.setUserName("zhangsan"); user.setPassword("zs"); Session session = sessionFactory.openSession(); session.beginTransaction(); // 开启事务 session.save(user); session.getTransaction().commit();// 提交事务 session.close(); } }
Hibernate5.1.fianl使用JPA注解方式异常:persistence.Table.indexes()[Ljavax/persistence/Index;
标签:
原文地址:http://blog.csdn.net/wangyang1354/article/details/50675687