开始学习hibernate。通过教程做所有事情,但当我尝试做简单的选择时,没有任何反应。不明白问题所在。
日志:
мар 28, 2016 12:54:44 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.1.0.Final}
мар 28, 2016 12:54:44 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
мар 28, 2016 12:54:44 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
мар 28, 2016 12:54:45 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
мар 28, 2016 12:54:45 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
мар 28, 2016 12:54:45 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/taskboard]
мар 28, 2016 12:54:45 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {user=root, password=****}
мар 28, 2016 12:54:45 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: false
мар 28, 2016 12:54:45 PM org.hibernate.engine.jdbc.connections.internal.PooledConnections <init>
INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
мар 28, 2016 12:54:45 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
мар 28, 2016 12:54:45 PM org.hibernate.validator.internal.util.Version <clinit>
INFO: HV000001: Hibernate Validator 5.2.4.Final
мар 28, 2016 12:54:45 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl stop
INFO: HHH10001008: Cleaning up connection pool [jdbc:mysql://localhost:3306/taskboard]
User.java
@Entity
@Table(name="Users")
public class User {
@Id
@Column(name="id_user")
private Long id;
@Column(name="first_name")
private String firstName;
@Column(name="last_name")
private String lastName;
@Column(name="login")
private String login;
@Column(name="password")
private String password;
}//get&set
Main.java
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.openSession();
List<User> users = null;
try {
session.beginTransaction();
Criteria criteria = session.createCriteria(User.class);
users = criteria.list();
session.getTransaction().commit();
} catch (Exception e) {
session.getTransaction().rollback();
e.printStackTrace();
} finally {
session.close();
sessionFactory.close();
}
for (User user: users) {
System.out.println(user.toString());
}
的hibernate.cfg.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/taskboard</property>
<property name="connection.username">root</property>
<property name="connection.password">1234</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<mapping class="User" />
</session-factory>
</hibernate-configuration>
HibernateUtil.java
public class HibernateUtil {
private static SessionFactory sessionFactory = null;
static {
Configuration cfg = new Configuration().configure();
StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder()
.applySettings(cfg.getProperties());
sessionFactory = cfg.buildSessionFactory(builder.build());
}
public static SessionFactory getSessionFactory(){
return sessionFactory;
}
}
答案 0 :(得分:0)
对于Users table,数据库可能没有任何内容。首先插入一些数据。然后检查。
答案 1 :(得分:0)
据我记忆,较新版本的hibernate不再支持hibernate配置文件中的public TextField l4M1ICT01;
public TextField l4M1ICT02;
public TextField l4M1CourseWork;
public TextField l4M1Average;
public int average_1;
public int credit_1;
public void l4M1AverageButtonClicked(ActionEvent event) throws IOException {
int l4M1ICT01_1 = Integer.parseInt(l4M1ICT01.getText());
int l4M1ICT02_1 = Integer.parseInt(l4M1ICT02.getText());
int l4M1CourseWork_1 = Integer.parseInt(l4M1CourseWork.getText());
average_1 = (l4M1ICT01_1 + l4M1ICT02_1 + l4M1CourseWork_1) / 3;
l4M1Average.setText(average_1 + "");
if(average_1 >= 40){
credit_1 = 20;
}else{
credit_1 = 0;
}
System.out.println(credit_1);
}
public void l4M1NextButtonClicked(ActionEvent event) throws IOException {
((Node) (event.getSource())).getScene().getWindow().hide();
//loading the next scene
Parent parent = FXMLLoader.load(getClass().getResource("L4M2.fxml"));
Stage stage = new Stage();
Scene scene = new Scene(parent);
stage.setScene(scene);
stage.setTitle("Level 4 Module 2");
//making the scene appear on the screen
stage.show();
}
public TextField l4M2ICT01;
public TextField l4M2ICT02;
public TextField l4M2CourseWork;
public TextField l4M2Average;
public int average_2;
public void l4M2AverageButtonClicked(ActionEvent event) throws IOException {
int l4M2ICT01_1 = Integer.parseInt(l4M2ICT01.getText());
int l4M2ICT02_1 = Integer.parseInt(l4M2ICT02.getText());
int l4M2CourseWork_1 = Integer.parseInt(l4M2CourseWork.getText());
average_2 = (l4M2ICT01_1 + l4M2ICT02_1 + l4M2CourseWork_1) / 3;
l4M2Average.setText(average_2 + "");
System.out.println(credit_1);
}
public void l4M2NextButtonClicked(ActionEvent event) throws IOException {
((Node) (event.getSource())).getScene().getWindow().hide();
//loading the next scene
Parent parent = FXMLLoader.load(getClass().getResource("L4M3.fxml"));
Stage stage = new Stage();
Scene scene = new Scene(parent);
stage.setScene(scene);
stage.setTitle("Level 4 Module 3");
//making the scene appear on the screen
stage.show();
}
。
你可以删除它,而不是尝试使用
您的<mapping class= .../>
静态区块中的
cfg.addAnnotatedClass(User.class)
。
另外,您可能需要考虑使用构建HibernateUtil
的最新方法,因为sessionFactory
方式已被弃用。有关详细信息,请参阅Native Bootstrapping部分。