gpt4 book ai didi

java - 实现 hibernate-CRUD wia 注释时出现 hbm.xml 未找到错误

转载 作者:行者123 更新时间:2023-12-01 12:59:31 25 4
gpt4 key购买 nike

我是 JPA 新手,我尝试使用 Hibernate 实现在 JPA 中设置一个项目。我研究发现我们可以使用注释或使用 *.hbm.xml 来编写代码。我试图用注释来实现它,但是当我尝试将值插入到我的数据库时,它会抛出如下错误。

资源:center/Centre.hbm.xml 未找到

我的 Centre.java 类如下

  /**
*
*/
package centre;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

/**
* @author anoop
*
*/

@Entity
@Table(name="centre")
public class Centre {

private int id;
private String name;
private String address;
private String comments;


/**
* @return the id
*/
@Id
public int getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(int id) {
this.id = id;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the address
*/
public String getAddress() {
return address;
}
/**
* @param address the address to set
*/
public void setAddress(String address) {
this.address = address;
}
/**
* @return the comments
*/
public String getComments() {
return comments;
}
/**
* @param comments the comments to set
*/
public void setComments(String comments) {
this.comments = comments;
}

}

持久性.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="gurukul">
<class>centre.Centre</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
</persistence-unit>
</persistence>

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">org.postgresql.Driver</property>
<property name="hibernate.connection.password">password</property>
<property name="hibernate.connection.url">jdbc:postgresql://127.0.0.1/DBNAME</property>
<property name="hibernate.connection.username">username</property>
<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="connection.pool_size">1</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">create</property>
</session-factory>
</hibernate-configuration>

HibernateUtil.java

    private static final SessionFactory sessionFactory;

static {
try {
sessionFactory = new AnnotationConfiguration().configure().addClass(Centre.class).buildSessionFactory();

} catch (Throwable ex) {
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}

public static SessionFactory getSessionFactory() {
return sessionFactory;
}

/**
* @return the em
*/
public static EntityManager getEm() {
return em;
}

GurukulDAO.java

  /**
*
*/
package dao;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;

import org.hibernate.Session;
import org.hibernate.Transaction;

import util.HibernateUtil;
import centre.Centre;

/**
* @author anoop
*
*/
public class GurukulDAO {

public void saveCentre(String name, String address, String comments){
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction txn = session.beginTransaction();

Centre centre = new Centre();
centre.setName(name);
centre.setAddress(address);
centre.setComments(comments);

session.save(centre);
txn.commit();

txn.begin();
}


}

Nainclass.java

 package main;

import dao.GurukulDAO;

public class MainClass {

public static void main(String s[]){
GurukulDAO gurukulDAO = new GurukulDAO();
gurukulDAO.saveCentre("BajajNagar", "Flat no. 5", "Head Office");
}

}

当我执行 MainClass 时,我收到错误

 1 [main] INFO org.hibernate.cfg.annotations.Version - Hibernate Annotations 3.4.0.GA
13 May, 2014 2:21:21 PM org.hibernate.cfg.Environment <clinit>
INFO: Hibernate 3.2 cr4
13 May, 2014 2:21:21 PM org.hibernate.cfg.Environment <clinit>
INFO: hibernate.properties not found
13 May, 2014 2:21:21 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: Bytecode provider name : cglib
13 May, 2014 2:21:21 PM org.hibernate.cfg.Environment <clinit>
INFO: using JDK 1.4 java.sql.Timestamp handling
13 May, 2014 2:21:21 PM org.hibernate.cfg.Configuration configure
INFO: configuring from resource: /hibernate.cfg.xml
13 May, 2014 2:21:21 PM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: Configuration resource: /hibernate.cfg.xml
13 May, 2014 2:21:21 PM org.hibernate.cfg.Configuration doConfigure
INFO: Configured SessionFactory: null
13 May, 2014 2:21:21 PM org.hibernate.cfg.Configuration addClass
INFO: Reading mappings from resource: centre/Centre.hbm.xml
Initial SessionFactory creation failed.org.hibernate.MappingNotFoundException: resource: centre/Centre.hbm.xml not found
Exception in thread "main" java.lang.ExceptionInInitializerError
at util.HibernateUtil.<clinit>(HibernateUtil.java:32)
at dao.GurukulDAO.saveCentre(GurukulDAO.java:22)
at main.MainClass.main(MainClass.java:9)
Caused by: org.hibernate.MappingNotFoundException: resource: centre/Centre.hbm.xml not found
at org.hibernate.cfg.Configuration.addClass(Configuration.java:538)
at org.hibernate.cfg.AnnotationConfiguration.addClass(AnnotationConfiguration.java:963)
at util.HibernateUtil.<clinit>(HibernateUtil.java:25)
... 2 more

请指导我在代码中做错了什么。

最佳答案

删除<class> persistence.xml 文件中的条目以及日志所报告的内容,您需要为 Hibernate 指定一个映射文件描述符,这可以通过添加 <mapping> 来完成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">org.postgresql.Driver</property>
<property name="hibernate.connection.password">password</property>
<property name="hibernate.connection.url">jdbc:postgresql://127.0.0.1/DBNAME</property>
<property name="hibernate.connection.username">username</property>
<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="connection.pool_size">1</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">create</property>
<mapping class="centre.Centre" /> <!-- You can also specify a whole package with <mapping package="centre" /> -->
</session-factory>
</hibernate-configuration>

关于java - 实现 hibernate-CRUD wia 注释时出现 hbm.xml 未找到错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23627082/

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