重拾Hibernate(1)之环境配置详解编程语言
2023-06-13 09:20:42 时间
2、导入所需jar包
3、Student.java
package com.blog.ytso.com; import javax.persistence.Entity; import javax.persistence.Table; @Entity @Table(name="T_STUDENT") public class Student extends BizEntity{ private String name; private String score; public String getName() { return name; public void setName(String name) { this.name = name; public String getScore() { return score; public void setScore(String score) { this.score = score;
Teacher.java
package com.blog.ytso.com; import javax.persistence.Entity; import javax.persistence.Table; @Entity @Table(name="T_TEACHER") public class Teacher extends BizEntity{ private String name; private String title; public String getName() { return name; public void setName(String name) { this.name = name; public String getTitle() { return title; public void setTitle(String title) { this.title = title;
BizEntity.java
package com.blog.ytso.com; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import org.hibernate.annotations.GenericGenerator; public class BizEntity { private String id; @Id @GeneratedValue(strategy=GenerationType.AUTO,generator="bizGenerator") @GenericGenerator(strategy = "uuid", name = "bizGenerator") public String getId() { return id; public void setId(String id) { this.id = id;BizEntity.java是所有实体类的父类,id自动生成策略为uuid
4、hibernate.hbm.xml
?xml version="1.0"? !DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" hibernate-mapping property name="dialect" org.hibernate.dialect.OracleDialect /property property name="connection.url" jdbc:oracle:thin:@172.16.1.4:1521:orcl /property property name="connection.username" username /property property name="connection.password" 123 /property property name="connection.driver_class" oracle.jdbc.driver.OracleDriver /property property name="current_session_context_class" thred /property property name="show_sql" true /property property name="format_sql" true /property property name="hbm2ddl.auto" true /property /hibernate-mapping5、Test.java测试
package com.blog.ytso.com; import org.hibernate.cfg.AnnotationConfiguration; import org.hibernate.tool.hbm2ddl.SchemaExport; public class Test { /** * @param args public static void main(String[] args) { // TODO Auto-generated method stub //new SchemaExport(new AnnotationConfiguration().configure()).create(true, true);Teacher t = new Teacher(); t.setName("张三"); Configuration cfg = new AnnotationConfiguration(); SessionFactory factory =cfg.configure().buildSessionFactory(); Session session = factory.openSession(); session.beginTransaction(); session.save(t); session.getTransaction().commit(); }SchemaExport自动生成数据表
运行程序:
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details. Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/impl/StaticLoggerBinder此错误是缺少slf4j-nop.x.jar包 添加一个该包(slf4j-nop-1.5.8.jar)
Exception in thread "main" java.lang.IllegalAccessError: tried to access field org.slf4j.impl.StaticLoggerBinder.SINGLETON from class org.slf4j.LoggerFactory
此错误是slf4j版本错误。slf4j-api是1.5.2而slf4j-nop是1.5.8
删除slf4j-api.1.5.2添加一个1.5.8的
Exception in thread "main" java.lang.NoClassDefFoundError: org/hibernate/annotations/common/reflection/ReflectionManager此错误是少一个jar包,引入hibernate-commons-annotations.jar
Caused by: org.xml.sax.SAXParseException: The content of element type "property" must match "(meta*,(column|formula)*,type?)". at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:236)此错误是hibernate-cfg.xml配置文件错误(刚开始我是从文档中copy的,而那个是mapping实体类的映射文件)
?xml version="1.0"? !DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd" hibernate-configuration session-factory property name="dialect" org.hibernate.dialect.OracleDialect /property property name="connection.url" jdbc:oracle:thin:@172.16.1.4:1521:orcl /property property name="connection.username" username /property property name="connection.password" 123 /property property name="connection.driver_class" oracle.jdbc.driver.OracleDriver /property property name="current_session_context_class" thred /property property name="show_sql" true /property property name="format_sql" true /property property name="hbm2ddl.auto" true /property /session-factory /hibernate-configuration
Exception in thread "main" org.hibernate.HibernateException: JDBC Driver class not found: oracle.jdbc.driver.OracleDriver没有引入oracle驱动
Exception in thread "main" org.hibernate.MappingException: Unknown entity: com.blog.ytso.com.Teacher没有在hibernate.cfg.xml中加入映射
mapping / mapping /然后运行:
Exception in thread "main" org.hibernate.AnnotationException: No identifier specified for entity: com.blog.ytso.com.Teacher[email protected] 用在实体的继承过程中的父类上。
如果直接再次运行的话 会报错 表或视图不存在
而应用SchemaExport先生成表
hibernate生成的表语句为:
drop table T_STUDENT cascade constraints drop table T_TEACHER cascade constraints create table T_STUDENT ( id varchar2(255) not null, name varchar2(255), score varchar2(255), primary key (id) create table T_TEACHER ( id varchar2(255) not null, name varchar2(255), title varchar2(255), primary key (id) )此时再插入一条teacher数据:
Hibernate: insert into T_TEACHER (name, title, id) values (?, ?, ?)最终所需jar包
结束
原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/14394.html
cjavaoraclexml
相关文章
- Apache配置详解(最好的APACHE配置教程)
- JAVA乐观锁_spring的线程池配置
- DSL和配置_ds3配置
- Mybatis01入门+使用和配置+面试题mybatis与hibernate的区别+ssm与ssh2开发对比
- 如何做好一个配置中心
- mycat分片配置测试
- online(3o/4o)配置ipv6-debian系统
- Spring Batch 配置方式-注解配置
- Win2008服务器或VPS安全配置基础教程
- hibernate链接数据库链接池c3p0配置详解编程语言
- Hibernate各种主键生成策略与配置详解编程语言
- MySQL Variables insert_id 数据库 参数变量解释及正确配置使用
- Hibernate hibernate.order_updates属性:为SQL更新排序
- FTP服务器配置在Linux系统上的步骤(ftp配置linux)
- 极速配置Redis环境变量(redis配置环境变量)
- 快速指南:Linux下如何删除网络配置(linux删除网络配置)
- 解决MySQL无法打开配置页面的问题(mysql不出现配置页面)
- windows下Apache+MySql+PHP3+PHP4+PERL安装配置