小弟刚刚毕业,来到公司,最近项目不是很忙,自己来学习下hibernate4 注解 , 写了一个 一对多,多对一的小例子。
公司用的maven 所以我也是建的maven工程,导入hibernate4的jar包
- <dependency>
- <groupId>org.hibernate</groupId>
- <artifactId>hibernate-core</artifactId>
- <version>4.1.6.Final</version>
- </dependency>
但是oracle的驱动包,好像要自己手动加。不知道有没有用maven直接从网上加的方法。
hibernate.cfg.xml 文件
- <!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="dialect">
- org.hibernate.dialect.Oracle10gDialect
- </property>
- <property name="connection.url">jdbc:oracle:thin:@192.168.15.102:1521:ora11g</property>
- <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
- <property name="connection.username">iris_ecnu_dev</property>
- <property name="connection.password">iris_ecnu_dev</property>
- <!-- 其他配置 -->
- <property name="show_sql">true</property>
- <property name="hbm2ddl.auto">update</property>
- <property name="format_sql">true</property>
- <!-- 导入映射配置 -->
- <mapping class="cn.lzg.Order" />
- <mapping class="cn.lzg.Person" />
- </session-factory>
- </hibernate-configuration>
Order.java 文件
- package cn.lzg;
- import java.util.Date;
- import javax.persistence.Column;
- import javax.persistence.Entity;
- import javax.persistence.FetchType;
- import javax.persistence.GeneratedValue;
- import javax.persistence.GenerationType;
- import javax.persistence.Id;
- import javax.persistence.JoinColumn;
- import javax.persistence.ManyToOne;
- import javax.persistence.SequenceGenerator;
- import javax.persistence.Table;
- @Entity
- @Table(name = "order_lzg")
- public class Order {
- @Id
- @SequenceGenerator(name = "order_lzg", sequenceName = "o_seq", allocationSize = 1)
- @GeneratedValue(generator = "order_lzg", strategy = GenerationType.SEQUENCE)
- private Long order_id;
- @Column(name = "submit_time")
- private Date submit_time;
- @ManyToOne(fetch = FetchType.LAZY)
- @JoinColumn(name = "p_id")
- // order_lzg表 里面 放person_lzg ID的列
- private Person person_lzg;
- public Order() {
- }
- public Long getOrder_id() {
- return order_id;
- }
- public void setOrder_id(Long order_id) {
- this.order_id = order_id;
- }
- public Date getSubmit_time() {
- return submit_time;
- }
- public void setSubmit_time(Date submit_time) {
- this.submit_time = submit_time;
- }
- public Person getPerson_lzg() {
- return person_lzg;
- }
- public void setPerson_lzg(Person person_lzg) {
- this.person_lzg = person_lzg;
- }
- }
Person.java 文件
- package cn.lzg;
- import java.util.HashSet;
- import java.util.Set;
- import javax.persistence.CascadeType;
- import javax.persistence.Column;
- import javax.persistence.Entity;
- import javax.persistence.FetchType;
- import javax.persistence.GeneratedValue;
- import javax.persistence.GenerationType;
- import javax.persistence.Id;
- import javax.persistence.OneToMany;
- import javax.persistence.SequenceGenerator;
- import javax.persistence.Table;
- @Entity
- @Table(name = "person_lzg")
- public class Person {
- @Id
- @SequenceGenerator(name = "person_lzg", sequenceName = "p_seq", allocationSize = 1)
- @GeneratedValue(generator = "person_lzg", strategy = GenerationType.SEQUENCE)
- private Long p_id;
- @Column(name = "name")
- private String name;
- @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "person_lzg")
- // mappedBy的值,是Order对象里面存Person对象的属性的值
- private Set<Order> orderSet = new HashSet<Order>();
- public Long getp_id() {
- return p_id;
- }
- public void setp_id(Long p_id) {
- this.p_id = p_id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public Set<Order> getOrderSet() {
- return orderSet;
- }
- public void setOrderSet(Set<Order> orderSet) {
- this.orderSet = orderSet;
- }
- }
本地测试类
- package cn.lzg;
- import java.util.Date;
- import org.hibernate.Session;
- import org.hibernate.SessionFactory;
- import org.hibernate.Transaction;
- import org.hibernate.cfg.Configuration;
- import org.hibernate.service.ServiceRegistry;
- import org.hibernate.service.ServiceRegistryBuilder;
- import org.junit.Test;
- public class TestHibernate {
- private static Configuration configuration = null;
- private static SessionFactory sessionFactory = null;
- private static ServiceRegistry serviceRegistry = null;
- @Test
- public void testSave() {
- /**
- * hibernate 4 貌失要这样获得sessionFactory 以前的configuration.buildSessionFactory();方法 过时了
- */
- configuration = new Configuration().configure();
- serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties())
- .buildServiceRegistry();
- sessionFactory = configuration.buildSessionFactory(serviceRegistry);
- Session session = sessionFactory.openSession();
- Transaction tx = session.beginTransaction();
- // tx.begin(); //这里不能再次开始,否则就事务嵌套了,会报异常
- Person p = new Person();
- p.setName("张三");
- Order o1 = new Order();
- o1.setSubmit_time(new Date());
- o1.setPerson_lzg(p);
- Order o2 = new Order();
- o2.setSubmit_time(new Date());
- o2.setPerson_lzg(p);
- p.getOrderSet().add(o1);
- p.getOrderSet().add(o2);
- session.save(p);
- // Person_Lzg p = (Person_Lzg) session.get(Person_Lzg.class, 5L); //先获得那条记录再删除 5L 是那条记录的ID
- // session.delete(p);
- tx.commit();
- session.close();
- }
- }
结果保存p, 两条订单的记录也能存进去。
删除的效果也一样。
另外有没有老鸟能指导下,现在做JAVA 或者是 JAVAWEB 主要要学些什么啊?有推荐的书,网站,资料最好啦。小弟才毕业,感觉很迷茫啊。