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();
}
}
- hibernat没有关联关系的添加记录和查询记录
- 单据关联关系记录
- spring+hibernat异常信息记录
- Mysql联合查询UNION和UNION ALL的使用介绍 可以查询出多张没有关联关系的表的数据
- A/B表替1对多关系,要求联合查询B表只提取一条与A记录关联的记录
- A/B表为1对多关系,要求联合查询B表只提取一条与A记录关联的记录
- Oracle 查询表/关联表中不重复的记录
- Oracle:select into 查询没有记录的解决办法
- .数据库、表的建立,记录的添加、查询、修改和删除
- mysql 关联查询 索引不起作用原因记录
- 去掉重复记录和查询重复记录的常见方法
- case when then和insert select组合查询添加记录
- django框架中,添加用户操作记录和查询功能
- 关于Yii1和Yii2的ActiveRecord活动记录对象关联查询时底层调用SQL语句的不同
- 查询记录中没有记录的就返回空,否则返回原值
- 查询和删除有重复的记录
- 重复记录的查询和删除
- Oracle的查询锁记录和解锁
- PHP获取字符串长度,中文标点或者中文汉字算一个字符
- 邻接表
- MySQL 简易序列
- ACM水题-排列(解题报告)
- Linux Kernel -- chess game
- hibernat没有关联关系的添加记录和查询记录
- dima_zy的打包PyQt程序
- mplayer
- 使用 ApacheBench 进行网站的压力测试
- C++ 重载赋值运算符
- PHP JSON格式数据交互实例详解
- ABAP里面游标的使用
- 抓过google的数据没?
- C#中五种跳转语句