在一开始导入hibernate框架时,导入了hibernate全部的包和c3p0数据源的包,结果在包名上产生了冲突,
eclipse无法自动正确导入,把鼠标移到 import org.hibernate*.;时出现
the package org.hibernate is accessible from more than one module:hibernate.c3p0,
hibernate.commons.annotations,hibernate.core;
当我把有冲突的包移除几个暂时不用的时候,没有编译错误,但运行的时候出现java.lang.NoClassDefFoundError
具体如下
Exception in thread "main" java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException
at org.hibernate.boot.spi.XmlMappingBinderAccess.(XmlMappingBinderAccess.java:43)
at org.hibernate.boot.MetadataSources.(MetadataSources.java:87)
at org.hibernate.cfg.Configuration.(Configuration.java:123)
at org.hibernate.cfg.Configuration.(Configuration.java:118)
at lee.NewsManager.main(NewsManager.java:30)
Caused by: java.lang.ClassNotFoundException: javax.xml.bind.JAXBException
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(Unknown Source)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(Unknown Source)
at java.base/java.lang.ClassLoader.loadClass(Unknown Source)
... 5 more
具体代码如下:
```package lee;
import org.hibernate.*;
import org.hibernate.cfg.*;
import org.hibernate.service.*;
import org.hibernate.boot.registry.*;
import org.crazyit.app.domain.New;
public class NewsManager
{
public static void main(String[] args)
throws Exception
{
// 实例化Configuration
Configuration conf = new Configuration()
// 不带参数的configure()方法默认加载hibernate.cfg.xml文件
// 如果传入abc.xml作为参数,则不再加载hibernate.cfg.xml,改为加载abc.xml
.configure();
// 以Configuration实例创建SessionFactory实例
SessionFactory sf = conf.buildSessionFactory();
// 创建Session
Session sess = sf.openSession();
// 开始事务
Transaction tx = sess.beginTransaction();
// 创建消息对象
New n = new New();
n.setContent(
+ "100");
sess.save(n);
// 提交事务
tx.commit();
// 关闭Session
sess.close();
sf.close();
}
}
package org.crazyit.app.domain;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.processing.Generated;
import javax.persistence.CollectionTable;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OrderColumn;
import javax.persistence.Table;
import javax.persistence.TableGenerator;
@Entity
@Table(name="news_inf")
public class New {
@TableGenerator(name="newsGen",table="NEWS_ID_GEN",pkColumnName="gen_key",valueColumnName="gen_value",pkColumnValue="new_id")
@GeneratedValue(strategy=GenerationType.TABLE,generator="newsGen")
private Integer id;
// 消息标题
private String title;
// 消息内容
private String content;
@ElementCollection (targetClass=String.class)
@CollectionTable(name="school_inf",joinColumns=@JoinColumn(name="person_id",nullable=false))
@Column(name="school_name")
@OrderColumn(name="list_order")
private List<String>schools=new ArrayList<>();
@ElementCollection (targetClass=Float.class)
@CollectionTable(name="score_inf",joinColumns=@JoinColumn(name="person_id",nullable=false))
@MapKeyColumn(name="subject_name")
@MapKeyClass(String.class)
@Column(name="mark")
// id的setter和getter方法
public void setId(Integer id)
{
this.id = id;
}
public Integer getId()
{
return this.id;
}
// title的setter和getter方法
public void setTitle(String title)
{
this.title = title;
}
public String getTitle()
{
return this.title;
}
@Generated(GenerationTime.ALWAYS)
@Column(name="full_content")
private String fullContent;
// content的setter和getter方法
public void setContent(String content)
{
this.content = content;
}
public String getContent()
{
return this.content;
}
}
配置文件<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<!-- 指定连接数据库所用的驱动 -->
com.mysql.jdbc.Driver
<!-- 指定连接数据库的url,其中hibernate是本应用连接的数据库名 -->
jdbc:mysql://localhost/hibernate?useSSL=true
<!-- 指定连接数据库的用户名 -->
root
<!-- 指定连接数据库的密码 -->
ak474247152919
<!-- 指定连接池里最大连接数 -->
20
<!-- 指定连接池里最小连接数 -->
1
<!-- 指定连接池里连接的超时时长 -->
5000
<!-- 指定连接池里最大缓存多少个Statement对象 -->
100
3000
2
true
<!-- 指定数据库方言 -->
org.hibernate.dialect.MySQL5InnoDBDialect
<!-- 根据需要自动创建数据表 -->
update<!--①-->
<!-- 显示Hibernate持久化操作所生成的SQL -->
true
<!-- 将SQL脚本进行格式化后再输出 -->
true
<!-- 罗列所有持久化类的类名 -->