我在mysql中有这个表:
CREATE TABLE IF NOT EXISTS `realEstate`.`USER` (
`userID` INT NOT NULL AUTO_INCREMENT,
`username` VARCHAR(45) NULL,
`password` VARCHAR(45) NULL,
`registrationDate` VARCHAR(15) NULL,
`name` VARCHAR(45) NULL,
`surname` VARCHAR(45) NULL,
`phone` VARCHAR(45) NULL,
`email` VARCHAR(45) NULL,
`registered` TINYINT(1) NULL,
PRIMARY KEY (`userID`),
UNIQUE INDEX `user_id_UNIQUE` (`userID` ASC))
ENGINE = InnoDB;
我尝试用hibernate获取所有内容。 Althoug我实现了.hbm文件,我得到the USER is not mapped[from USER]
的错误。我在这里如何提出问题:
List<Users> users = null;
Session session = null;
Transaction transaction = null;
try {
SessionFactory factory = HibernateUtil.getSessionFactory();
session = factory.getCurrentSession();
transaction = session.beginTransaction();
Query query = session.createQuery("from USER");
users = query.list();
transaction.commit();
} catch (HibernateException ex) {
System.out.println("Exception:");
System.out.println(ex.getMessage());
if (transaction != null) {
transaction.rollback();
}
} finally {
/*for(Users user:users){
System.out.println(user.getName());
}*/
}
System.out.println("Complete");
这是users.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>
<class name="Users" table="USER">
<id name="userID" type="int">
<column name="userID" />
<generator class="assigned" />
</id>
<property name="username" type="java.lang.String">
<column name="username" length="45"/>
</property>
<property name="password" type="java.lang.String">
<column name="password" length="45" />
</property>
<property name="name" type="java.lang.String">
<column name="name" length="45"/>
</property>
<property name="date" type="java.lang.String">
<column name="registrationDate" length="45"/>
</property>
<property name="surname" type="java.lang.String">
<column name="surname" length="45"/>
</property>
<property name="phone" type="java.lang.String">
<column name="phone" length="45"/>
</property>
<property name="email" type="java.lang.String">
<column name="email" length="45"/>
</property>
<property name="registered" type="boolean">
<column name="registered" />
</property>
</class>
</hibernate-mapping>
Eveyrything对我来说似乎很好,但它不起作用。你能救我吗?
这里是用户类:
public class Users {
private int user_id;
private String username;
private String password;
private String date;
private String name;
private String surname;
private String phone;
private String email;
private boolean registered;
public int getuserID() {
return user_id;
}
public void setuserID(int user_id) {
this.user_id = user_id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public boolean getRegistered() {
return registered;
}
public void setRegistered(boolean registered) {
this.registered = registered;
}
}
这里是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 name="session1">
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password"></property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/realestate</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.current_session_context_class">thread</property>
<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.max_size">20</property>
<property name="hibernate.c3p0.timeout">300</property>
<property name="hibernate.c3p0.max_statements">50</property>
<property name="hibernate.c3p0.idle_test_period">3000</property>
<mapping resource="users.hbm.xml"/>
<mapping resource="adminstrator.hbm.xml"/>
</session-factory>
</hibernate-configuration>
以及目录
答案 0 :(得分:1)
在HQL
中,您应该使用映射的Entity
的java类名和属性名,而不是实际的表名和列名,因此HQL
应该是:< / p>
Query query = session.createQuery("from USERS");
您在users.hbm.xml
中提供的