首先需要使用Hibernate逆向生成实体类。
参考文章:点击打开
我这里有两张表。
数据如下:
整个工程结构如下:
MySessionFactory.java:
package entity;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class MySessionFactory {
// 加载hibernate.cfg.xml
private static final Configuration cfg = new Configuration().configure("hibernate.cfg.xml");
private static SessionFactory sessionFactory;
private static Session session;
public static Session getSession() throws HibernateException {
if (session == null || session.isOpen() == false) {
if (sessionFactory == null) {
try {
sessionFactory = cfg.buildSessionFactory();
} catch (Exception e) {
e.printStackTrace();
}
}
session = sessionFactory.openSession();
}
return session;
}
}
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="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://127.0.0.1:3306/test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 加上两个实体类的映射 -->
<mapping resource="entity/Username.hbm.xml" />
<mapping resource="entity/Password.hbm.xml"/>
</session-factory>
</hibernate-configuration>
hibernate.cfg.xml是逆向工具自动生成的。不过我们得加上<map resource="...">
查询例子:
package entity;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.osgi.service.useradmin.User;
public class Main {
@SuppressWarnings("unchecked")
public static void main(String[] args) {
Session session = MySessionFactory.getSession();
Transaction transaction = session.beginTransaction();
List<Username> list = session.createQuery("from Username u, Password p where u.id = p.id and u.id = ?").setParameter(0, 2).list();
Iterator<?> iterator = list.iterator();
while(iterator.hasNext()) {
Object[] objects = (Object[]) iterator.next();
Username username = (Username) objects[0];
Password passwordId = (Password) objects[1];
System.out.println(username.getId() + "\t" + username.getUsername() + "\t" + passwordId.getPassword());
}
session.close();
}
}
【注意.setParameter(0, 1)方法是从0开始计算】
控制台输出:
增加例子:
Username username = new Username();
username.setId(4);
username.setUsername("Aiden4");
session.save(username);
transaction.commit();
Navicat数据反馈:
【注意:一定要调用commit方法,才会生效】
修改例子:
Username username = new Username();
username.setId(2);
username.setUsername("Aiden3");
session.update(username);
transaction.commit();
Navicat数据反馈:
删除例子:
Username username = new Username();
username.setId(2);
username.setUsername("Aiden3");
session.delete(username);
transaction.commit();
Navicat数据反馈:
补充下:
刚博主运行了有个错误
Exception in thread "main" org.hibernate.NonUniqueObjectException: A different object with the same identifier value was already associated with the session : [entity.Username#1]
错误的原因就是:session本来携带了数据,比如说session已经做了查询username.id=2的操作,则session中存在了Username id为2的对象。
如果此时继续
Username username = new Username(); username.setId(2); username.setUsername("Aiden3"); session.update(username); transaction.commit();
则会产生错误。
解决的办法:调用session.clear方法。删除session携带的数据,再去做增删改查动作。
文章最后附上Hibernate增删改查的例子:
附件下载:点击下载