我有一个使用hibernate功能查询数据库的数据库和java应用程序。有一个名为NCS_User的类,其参数正确地映射到xml文件中的数据库。我使用以下代码从外部填充的数据库中提取数据。
package testhibernate;
public class NCS_User {
private String PERSONIDNO;
private String FULLNAME;
private int ROLEID;
private String ROLENAME;
private int SCHEMEID;
private String SCHEMETITLE;
/**
* creates the system default users [only used by spring]
*
* @param PERSONIDNO
* @param FULLNAME
* @param ROLEID
* @param ROLENAME
* @param SCHEMEID
* @param SCHEMETITLE
*/
private NCS_User(final int ROLEID) {
this.ROLEID = ROLEID;
}
public String getFULLNAME() {
return FULLNAME;
}
public int getROLEID() {
return ROLEID;
}
public String getROLENAME() {
return ROLENAME;
}
public int getSCHEMEID() {
return SCHEMEID;
}
public String getSCHEMETITLE() {
return SCHEMETITLE;
}
public String getPERSONIDNO() {
return PERSONIDNO;
}
public void setFULLNAME(final String FULLNAME) {
this.FULLNAME = FULLNAME;
}
public void setROLEID(final int ROLEID) {
this.ROLEID = ROLEID;
}
public void setROLENAME(final String ROLENAME) {
this.ROLENAME = ROLENAME;
}
public void setSCHEMEID(final int SCHEMEID) {
this.SCHEMEID = SCHEMEID;
}
public void setSCHEMETITLE(final String SCHEMETITLE) {
this.SCHEMETITLE = SCHEMETITLE;
}
public void setPERSONIDNO(final String PERSONIDNO){
this.PERSONIDNO = PERSONIDNO;
}
}
package testhibernate;
import org.hibernate.Session;
import org.hibernate.*;
import org.hibernate.cfg.*;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import java.util.*;
/**
*
* @author Administrator
*/
public class TestHibernate {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
System.out.println("Entering main loop");
Session session = null;
int qroleId=0;
int qschemeId=0;
String trueLogin="S1234567D";
try{
System.out.println("Establishing connection");
// This step will read hibernate.cfg.xml and prepare hibernate for use
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
session =sessionFactory.openSession();
String SQL_QUERY ="Select ncs_user.PERSONIDNO, ncs_user.ROLEID, ncs_user.SCHEMEID from NCS_User ncs_user where ncs_user.PERSONIDNO="+trueLogin;
Query query = session.createQuery(SQL_QUERY);
System.out.println("Query created");
for(Iterator it=query.iterate();it.hasNext();){
Object[] row = (Object[]) it.next();
qroleId = new Integer(row[1].toString());
System.out.println("Role Id: " + qroleId);
qschemeId = new Integer(row[2].toString());
System.out.println("Scheme Id: " + qschemeId);
}
}
catch(Exception e){
System.out.println(e.getMessage());
}
finally{
}
}
}
<?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="dialect">org.hibernate.dialect.Oracle12cDialect</property>
<property name="connection.driver_class">oracle.jdbc.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
<property name="connection.username">system</property>
<property name="connection.password">******</property>
<property name="cache.provider_class">org.hibernate.cache.HashtableCacheProvider</property>
<property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<property name="current_session_context_class">thread</property>
<property name="hibernate.show_sql">true</property>
<!-- Mapping files -->
<mapping resource="NCS_User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
<?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>
<class name="TestHibernate.NCS_User" table="NCS_User">
<id name="ROLEID" type="long" column="ROLEID" >
<generator class="assigned"/>
</id>
<property name="FULLNAME">
<column name="FULLNAME" />
</property>
<property name="PERSONIDNO">
<column name="PERSONIDNO"/>
</property>
<property name="ROLENAME">
<column name="ROLENAME"/>
</property>
<property name="SCHEMEID">
<column name="SCHEMEID"/>
</property>
<property name="SCHEMETITLE">
<column name="SCHEMETITLE"/>
</property>
</class>
</hibernate-mapping>
但是,当我尝试打印qroleID和qschemeId时,我没有得到任何值。在命令提示符中,我收到错误“无法从资源NCS_User.hbm.xml解析映射文档” 这是在“建立连接”打印出来之后。 有人可以帮我理解我在这里做错了什么吗?谢谢。
答案 0 :(得分:1)
包名称(也作为名称属性值的一部分)区分大小写。
您之间存在不匹配:
<class name="TestHibernate.NCS_User" table="NCS_User">
和
package testhibernate;
...
public class NCS_User
答案 1 :(得分:0)
使用create SQL Query而不是createQuery
String SQL_QUERY = "Select ncs_user.PERSONIDNO as pId, ncs_user.ROLEID as rId, ncs_user.SCHEMEID as sId from NCS_User ncs_user where ncs_user.PERSONIDNO="+trueLogin";
SQLQuery query = session.createSQLQuery(SQL_QUERY);
query.addScalar("pId", Hibernate.LONG)
query.addScalar("rId", Hibernate.LONG)
query.addScalar("sId", Hibernate.LONG)
List<Object[]> result = query.list();
for (Object[] objects : result) {
long pId = ((Integer) objects[0]).longValue();
long rId = ((Integer) objects[1]).longValue();
long sId = ((Integer) objects[2]).longValue();
}
可能会对你有帮助。 我不确定这段代码。