gpt4 book ai didi

java - 未找到映射(资源)

转载 作者:太空宇宙 更新时间:2023-11-04 12:28:50 26 4
gpt4 key购买 nike

伙计们,我是 Hibernate 新手,我正在努力使 hello world 应用程序正常工作。下面我将发布我遇到的异常以及我的代码 - 希望有人能够发现我遇到的其他错误并将我从“ future ”的麻烦中拯救出来。

代码:

package org.arpit.javapostsforlearning;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity(name = "User_table")
public class User {

@Id
int userId;

@Column(name = "User_Name")
String userName;

String userMessage;

public int getUserId() {
return userId;
}

public void setUserId(int userId) {
this.userId = userId;
}

public String getUserName() {
return userName;
}

public void setUserName(String userName) {
this.userName = userName;
}

public String getUserMessage() {
return userMessage;
}

public void setUserMessage(String userMessage) {
this.userMessage = userMessage;
}

}

这个:

package org.arpit.javapostsforlearning;

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

public class HibernateMain {
public static void main(String[] args)
{
Configuration configuration=new Configuration();
configuration.configure();
configuration.addClass(org.arpit.javapostsforlearning.User.class);

ServiceRegistry sr= new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
SessionFactory sf=configuration.buildSessionFactory(sr);

User user1=new User();
user1.setUserName("Arpit");
user1.setUserMessage("Hello world from arpit");

User user2=new User();
user2.setUserName("Ankita");
user2.setUserMessage("Hello world from ankita");

Session ss=sf.openSession();

ss.beginTransaction(); //saving objects to session
ss.save(user1);
ss.save(user2);
ss.getTransaction().commit();
ss.close();

}

}

和 XMLS:

<?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>

<!-- Database connection settings -->
<property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
<property name="connection.url">jdbc:sqlserver://localhost:1433;database=Sandboxlearning;integratedSecurity=true</property>


<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>

<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.SQLServer2005Dialect</property>

<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>

<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create</property>

<mapping class="User.hbm.xml"/>

</session-factory>

</hibernate-configuration>

用户.hbm.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
<class name="User" table="User_table">
<meta attribute="class-description">
This class contains the user detail.
</meta>
<id name="userId" type="int" column="userId">
<generator class="native"/>
</id>
<property name="userName" column="User_Name" type="string"/>
<property name="userMessage" column="userMessage" type="string"/>
</class>
</hibernate-mapping>

异常:

Jun 28, 2016 7:17:15 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.2.0.Final}
Jun 28, 2016 7:17:15 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Jun 28, 2016 7:17:15 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Jun 28, 2016 7:17:15 PM org.hibernate.boot.jaxb.internal.stax.LocalXmlResourceResolver resolveEntity
WARN: HHH90000012: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/hibernate-configuration. Use namespace http://www.hibernate.org/dtd/hibernate-configuration instead. Support for obsolete DTD/XSD namespaces may be removed at any time.
Exception in thread "main" org.hibernate.boot.MappingNotFoundException: Mapping (RESOURCE) not found : org/arpit/javapostsforlearning/User.hbm.xml : origin(org/arpit/javapostsforlearning/User.hbm.xml)
at org.hibernate.boot.spi.XmlMappingBinderAccess.bind(XmlMappingBinderAccess.java:56)
at org.hibernate.boot.MetadataSources.addResource(MetadataSources.java:274)
at org.hibernate.boot.MetadataSources.addClass(MetadataSources.java:262)
at org.hibernate.cfg.Configuration.addClass(Configuration.java:513)
at org.arpit.javapostsforlearning.HibernateMain.main(HibernateMain.java:14)

XMLS 存储在 src 文件夹下。

这也是我的应用程序的屏幕截图

image

最佳答案

查看:<mapping class="User.hbm.xml"/> 。但你使用资源。当您说映射我的类时,您可以使用类,但您想要映射资源。所以只需使用 <mapping resource="User.hbm.xml"/>

关于java - 未找到映射(资源),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38080293/

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