hibernat没有关联关系的添加记录和查询记录

来源:互联网 发布:篦子 淘宝 编辑:程序博客网 时间:2024/05/20 19:46

//hibernate.cfg.xml的代码

<!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="foo">
 <property name="show_sql">true</property>
 <property name="myeclipse.connection.profile">mysql</property>
 <property name="connection.url">jdbc:mysql://localhost:3306/caohuan</property>
 <property name="connection.username">root</property>
 <property name="connection.password">caohuan</property>
 <property name="connection.driver_class">
  com.mysql.jdbc.Driver
 </property>
 <property name="dialect">
  org.hibernate.dialect.MySQLDialect
 </property>
 
 <property name="hibernate.hbm2ddl.auto">update</property>
 <mapping resource="com/test/User.hbm.xml" />
 
</session-factory>
</hibernate-configuration>

 

//User.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
 package="com.test">

 <class name="User" table="User">
 <id name="id" column="id">
  <generator class="native"></generator>
  
 </id>
 <property name="age"></property>
 <property name="birth"></property>
 <property name="name"></property>
  
  
 </class>
 
</hibernate-mapping>

 

//User的代码

import java.util.Date;

public class User {
 private Integer id;
 private String name;
 private Date birth;
 private Integer age;
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public Date getBirth() {
  return birth;
 }
 public void setBirth(Date birth) {
  this.birth = birth;
 }
 public Integer getAge() {
  return age;
 }
 public void setAge(Integer age) {
  this.age = age;
 }
 public Integer getId() {
  return id;
 }
 public void setId(Integer id) {
  this.id = id;
 }
 @Override
 public String toString() {
  // TODO Auto-generated method stub
  return "id=" + getId() + ", name=" + getName() + ", age=" + getAge() + ", birth=" + getBirth();
 }
 

}

//这是测试

import java.util.Date;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;

public class Main {

 
 public static void main(String[] args) {
  
  Configuration con = new Configuration().configure();
//  SchemaExport export = new SchemaExport(con);
//  export.create(true, true);
  SessionFactory factory = con.buildSessionFactory();
  Session session = factory.openSession();
  Transaction tr = session.beginTransaction();
//  User user = new User();
//  user.setAge(21);
//  user.setBirth(new Date());
//  user.setName("�ܻ�");
//  session.save(user);
  Query query = session.createQuery("from User where age = 21");
  List<User> list = query.list();
  System.out.println(list.size());
  for (User user : list) {
   System.out.println(user.toString());
  }
  tr.commit();
  session.close();
 }
}

原创粉丝点击