gpt4 book ai didi

java - Hibernate 无法运行 session

转载 作者:行者123 更新时间:2023-12-01 12:04:26 27 4
gpt4 key购买 nike

运行此文件时遇到的错误

run:
Going to run Session!
Jan 01, 2015 3:38:25 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.4.Final}
Jan 01, 2015 3:38:25 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.3.1.Final}
Jan 01, 2015 3:38:25 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Jan 01, 2015 3:38:25 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Jan 01, 2015 3:38:25 PM org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
Jan 01, 2015 3:38:25 PM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
Jan 01, 2015 3:38:25 PM org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity
WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
Jan 01, 2015 3:38:25 PM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: Classes/Data.hbm.xml
Jan 01, 2015 3:38:25 PM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: hibernate.hbm.xml
Jan 01, 2015 3:38:25 PM org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity
WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
Exception in thread "main" org.hibernate.MappingException: <mapping> element in configuration specifies no known attributes
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:2286)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:2227)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2207)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2160)
at org.hibernate.cfg.Configuration.configure(Configuration.java:2075)
at org.hibernate.cfg.Configuration.configure(Configuration.java:2054)
at org.hibernate.util.HibernateUtil.getSessionFactory(HibernateUtil.java:14)
at test.Test.main(Test.java:12)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)

我的代码中的其他文件,其中 Test.java 是我的主文件

测试.java

package test;
import org.hibernate.Session;
import org.hibernate.Query;
import java.util.*;
import org.hibernate.util.HibernateUtil;


public class Test {



public static void main(String[] args) {
System.out.println("Going to run Session!");
Session session= HibernateUtil.getSessionFactory().openSession();
System.out.println("Going to run Query!");
Query query = session.createQuery("from Data");
System.out.println("Going to run save query!");
List list = query.list();
System.out.println("Going to print list!");
System.out.print(list);
}
}

hibernateUtil.java

package org.hibernate.util;

import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

public class HibernateUtil {
private static SessionFactory sessionFactory;
private static ServiceRegistry serviceRegistry;

public static SessionFactory getSessionFactory() {
Configuration configuration = new Configuration();
configuration.configure();
serviceRegistry = new StandardServiceRegistryBuilder().applySettings(
configuration.getProperties()).build();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
return sessionFactory;
}
}

hibernate.cfg.xml

    <?xml version="1.0" encoding="UTF-8"?>
<!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="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
<property name="hibernate.connection.url">jdbc:sqlserver://localhost\I7\SQLEXPRESS:1433;databaseName=bulksms</property>
<property name="hibernate.connection.username">connect</property>
<property name="hibernate.connection.password">connection</property>
<mapping resource="Classes/Data.hbm.xml"/>
<mapping resource="hibernate.hbm.xml"/>
<mapping/>
<mapping/>
</session-factory>
</hibernate-configuration>

hibernate.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
<class name="Classes.Data" table="Data" schema="dbo" catalog="bulksms" optimistic-lock="version">
<id name="name" type="string">
<column name="Name" length="20" />
<generator class="assigned" />
</id>
<property name="cell" type="string">
<column name="Cell" length="12" />
</property>
</class>
</hibernate-mapping>

hibernate.reveng.xml

    <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-reverse-engineering PUBLIC "-//Hibernate/Hibernate Reverse Engineering DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-reverse-engineering-3.0.dtd">
<hibernate-reverse-engineering>
<schema-selection match-catalog="bulksms" match-schema="dbo"/>
<table-filter match-name="Data"/>
</hibernate-reverse-engineering>

数据.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- Generated 30 Dec 2014 8:25:05 PM by Hibernate Tools 4.3.1 -->
<hibernate-mapping>
<class name="Classes.Data" table="Data" schema="dbo" catalog="bulksms" optimistic-lock="version">
<id name="name" type="string">
<column name="Name" length="20" />
<generator class="assigned" />
</id>
<property name="cell" type="string">
<column name="Cell" length="12" />
</property>
</class>
</hibernate-mapping>

数据.java

package Classes;
// Generated 30 Dec 2014 8:25:04 PM by Hibernate Tools 4.3.1

/**
* Data generated by hbm2java
*/
public class Data implements java.io.Serializable {


private String name;
private String cell;

public Data() {
}

public Data(String name) {
this.name = name;
}
public Data(String name, String cell) {
this.name = name;
this.cell = cell;
}

public String getName() {
return this.name;
}

public void setName(String name) {
this.name = name;
}
public String getCell() {
return this.cell;
}

public void setCell(String cell) {
this.cell = cell;
}




}

最佳答案

基于此错误消息:

<mapping> element in configuration specifies no known attributes

问题似乎出在空<mapping> Hibernate 配置文件中的元素:

<mapping/>
<mapping/>

您应该删除它们。

此外,虽然这不是错误,但日志中有警告您正在使用过时的命名空间。理想情况下,您也应该解决这个问题。

关于java - Hibernate 无法运行 session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27731301/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com