码迷,mamicode.com
首页 > 编程语言 > 详细

Hibernate5.1.fianl使用JPA注解方式异常:persistence.Table.indexes()[Ljavax/persistence/Index;

时间:2016-02-16 20:50:41      阅读:309      评论:0      收藏:0      [点我收藏+]

标签:

之前开发时我用的是配置文件的方式,实现实体类和数据库中数据的映射关系,后来看到也有注解的方式,也比较方便,就想试试,去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)

解决方案

1.  问题:第一行的这个信息Disabling contextual LOB creation as JDBC driver reported JDBC version [3] less than 4。
    解释:禁用上下文LOB创建JDBC驱动程序的JDBC报道版本[3]小于4,也就是说表明了当前你用的驱动程序版本太低,Hibernate加载后处理信息的时候会发现版本低于要求的版本,这里不是异常但是我感觉需要注意一下。
    解决:下载高本版的连接驱动程序,当然要与数据库版本相匹配。

2. 问题:java.lang.NoSuchMethodError: javax.persistence.Table.indexes()[Ljavax/persistence/Index;
实体类对象的注解部分代码:
@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;
	}

解决:将实体上面的注解直接改为这样:@Entity(name = "user"),然后就可以了。

程序重要代码:
配置文件:hibernate.cfg.xml
<?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>

User实体类
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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!