我正在尝试使用Hibernate以便从MySQL数据库中获取数据。为了实现这样一个目标,我创造了:
>
Hibernate配置文件:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="hibernate.connection.url">
jdbc:mysql://localhost:3306/employeesDbAF
</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password"></property>
<property name="hibernate.connection.pool_size">50</property>
<property name="show_sql">true</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="hibernate.hbm2ddl.auto">validate</property>
<mapping class="employeesapp.Employee"></mapping>
</session-factory>
</hibernate-configuration>
public class HibernateUtil {
private static SessionFactory sessionFactory;
private static ServiceRegistry serviceRegistry;
public static SessionFactory createSessionFactory() {
Configuration configuration = new Configuration();
configuration.configure();
serviceRegistry = new StandardServiceRegistryBuilder().applySettings(
configuration.getProperties()).build();
//sessionFactory = configuration.buildSessionFactory(serviceRegistry);
sessionFactory = configuration.buildSessionFactory();
return sessionFactory;
}
public static void close() {
StandardServiceRegistryBuilder.destroy(serviceRegistry);
}
}
public static void main(String[] args) {
Session session = HibernateUtil.createSessionFactory().openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
Employee emp = (Employee)session.get(Employee.class, 2);
System.out.println(emp);
tx.commit();
} catch (HibernateException e) {
if (tx != null) {
tx.rollback();
}
System.out.println(e);
} finally {
HibernateUtil.close();
}
}
@Entity
@Table(name="employee")
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="employee_ID")
private int employee_ID;
@Column(name="name")
private String name;
@Column(name="age")
private int age;
@Column(name="address")
private String address;
@Column(name="salary")
private int salary;
public int getEmployee_ID() {return employee_ID;}
public void setEmployee_ID(int employee_ID) {this.employee_ID = employee_ID; }
public String getName() {return name; }
public void setName(String name) {this.name = name; }
public int getAge() {return age;}
public void setAge(int age) {this.age = age; }
public String getAddress() {return address; }
public void setAddress(String address) {this.address = address; }
public int getSalary() {return salary; }
public void setSalary(int salary) {this.salary = salary; }
public Employee(int employee_ID, String name, int age, String address, int salary) {
//this.employee_ID = employee_ID;
this.name = name;
this.age = age;
this.address = address;
this.salary = salary;
}
public Employee(){
}
}
run:
Jun 05, 2019 10:53:26 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.4.Final}
Jun 05, 2019 10:53:26 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.3.1.Final}
Jun 05, 2019 10:53:26 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Jun 05, 2019 10:53:26 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Jun 05, 2019 10:53:26 PM org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
Jun 05, 2019 10:53:26 PM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
Jun 05, 2019 10:53:26 PM org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: null
Jun 05, 2019 10:53:26 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH000402: Using Hibernate built-in connection pool (not for production use!)
Jun 05, 2019 10:53:26 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000401: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/employeesDbAF]
Jun 05, 2019 10:53:26 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000046: Connection properties: {user=root, password=****}
Jun 05, 2019 10:53:26 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000006: Autocommit mode: false
Jun 05, 2019 10:53:26 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000115: Hibernate connection pool size: 50 (min=1)
Jun 05, 2019 10:53:27 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
Jun 05, 2019 10:53:27 PM org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService
INFO: HHH000399: Using default transaction strategy (direct JDBC transactions)
Jun 05, 2019 10:53:27 PM org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>
INFO: HHH000397: Using ASTQueryTranslatorFactory
Jun 05, 2019 10:53:27 PM org.hibernate.tool.hbm2ddl.SchemaValidator validate
INFO: HHH000229: Running schema validator
Jun 05, 2019 10:53:27 PM org.hibernate.tool.hbm2ddl.SchemaValidator validate
INFO: HHH000102: Fetching database metadata
Jun 05, 2019 10:53:27 PM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000261: Table found: employeesDbAF.employee
Jun 05, 2019 10:53:27 PM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000037: Columns: [address, employee_id, name, salary, age]
**Hibernate: select employee0_.employee_ID as employee1_0_0_, employee0_.address as address2_0_0_, employee0_.age as age3_0_0_, employee0_.name as name4_0_0_, employee0_.salary as salary5_0_0_ from employee employee0_ where employee0_.employee_ID=?
employeesapp.Employee@53b7f657**
必须重写Employee类中的公共字符串toString()
方法。
@Override
public String toString() {
return "Employee{" +
"employee_ID=" + employee_ID +
", name='" + name + '\'' +
", age=" + age +
", address='" + address + '\'' +
", salary=" + salary +
'}';
}
system.out.println(emp);
方法只需打印即可控制台toString()方法返回值
Employee@53b7f657
它会在内部调用,
// java.io.PrintStream
public void println(Object x) {
String s = String.valueOf(x);
synchronized (this) {
print(s);
newLine();
}
}
// java.lang.String
public static String valueOf(Object obj) {
return (obj == null) ? "null" : obj.toString();
}
// java.lang.Object
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
来源https://docs.oracle.com/javase/tutorial/java/iandi/subclasses.html
问题内容: 我最近开始学习Hibernate技术,不得不使用Hibernate从数据库中获取数据。问题是我只能通过SSH隧道连接到数据库。我可以在文件中使用任何属性来解决此问题吗?或者,您可以建议另一种新手可以理解的方式。 问题答案: 也许使用Jsch。一些示例可以为您提供解决之道。 另一种方法是使用HTTP代理处理程序和端口转发功能来实现自己的SSH SocketFactory。起点可以是: 您
我正试图从表中获取数据,但我没有得到任何东西,只是日志文件中的一些错误...请有人帮助我 Catalina.2013-11-15.log 2013年11月15日11:13:32.568信息[http-apr-8080-exec-2]null.null hhh000206:hibernate.properties找不到 2013年11月15日11:13:32.577信息[http-apr-8080-
我正在为实体对象上的延迟加载集合执行此操作: 我想返回一个实体对象,其中加载了多个延迟加载的集合,我可以这样做吗(传入一个列表并为单个条件设置多个关联?):
我似乎无法让我的Spring JPA配置正常工作。我有一个Spring REST服务。如果我将所有实体设置为FetchType.EAGER,一切都按预期工作。但是我不想这样做,原因很明显。 当我将实体设置为FetchType时。懒惰,我得到以下错误: org.hibernate.LazyInitializationException:未能懒惰地初始化角色集合:com.service.entitie
我已经引用了这个... Android HttpClient、DefaultHttpClient、HttpPost、http://www.androidsnippets.com/executing-a-http-post-request-with-httpclient和http://www.javacodegeeks.com/2013/06/android-apache-http-client.h
问题内容: 我有一个实体,该实体具有必须从序列中设置的NON-ID字段。目前,我获取序列的第一个值,将其存储在客户端,然后根据该值进行计算。 但是,我正在寻找一种“更好”的方法。我实现了一种获取下一个序列值的方法: 但是,这种方式会大大降低性能(〜5000个对象的创建速度降低了3倍-从5740ms到13648ms)。 我试图添加一个“假”实体: 但是,该方法也不起作用(返回的所有ID为0)。 有人