浏览 1888 次
锁定老帖子 主题:hibernate jpa基本映射
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2012-03-20
了解常用注解: @Entity @Id @Table @GeneratedValue @Transient @Column
采用如下注解会默认采用数据库的主键生成能力,相当于hibernate中的native @GeneratedValue相当于@GeneratedValue(strategy=GenerationType.AUTO)
了解如何采用hibernate的uuid生成主键
package com.hjc.hibernate; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Table; import javax.persistence.Transient; @Entity @Table(name="t_user1") public class User1 { private int id; private String name; private int age; @Id //采用数据库自增方式生成主键 //@GeneratedValue(strategy=GenerationType.AUTO) @GeneratedValue public int getId() { return id; } public void setId(int id) { this.id = id; } @Column(name="user_name", length=30, nullable=false, unique=true) public String getName() { return name; } public void setName(String name) { this.name = name; } //年龄不持久化 @Transient public int getAge() { return age; } public void setAge(int age) { this.age = age; } } package com.hjc.hibernate; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name="t_user2") public class User2 { private int id; private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } @Id @GeneratedValue(strategy=GenerationType.IDENTITY) public int getId() { return id; } public void setId(int id) { this.id = id; } }
package com.hjc.hibernate; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name="t_user3") public class User3 { private int id; private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } @Id @GeneratedValue(strategy=GenerationType.TABLE) public int getId() { return id; } public void setId(int id) { this.id = id; } } package com.hjc.hibernate; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Table; import org.hibernate.annotations.GenericGenerator; @Entity @Table(name="t_user4") public class User4 { private String id; private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } //采用hibernate uuid生成器 @Id @GenericGenerator(name="myIdGenerator",strategy="uuid") @GeneratedValue(generator="myIdGenerator") public String getId() { return id; } public void setId(String id) { this.id = id; } }
public void testSave1() { Session session = null; try { session = HibernateUtils.getSession(); session.beginTransaction(); User1 user = new User1(); user.setName("李四11"); session.save(user); session.getTransaction().commit(); }catch(Exception e) { e.printStackTrace(); session.getTransaction().rollback(); }finally { HibernateUtils.closeSession(session); } } public void testSave2() { Session session = null; try { session = HibernateUtils.getSession(); session.beginTransaction(); User2 user = new User2(); user.setName("李四"); session.save(user); session.getTransaction().commit(); }catch(Exception e) { e.printStackTrace(); session.getTransaction().rollback(); }finally { HibernateUtils.closeSession(session); } } public void testSave3() { Session session = null; try { session = HibernateUtils.getSession(); session.beginTransaction(); User3 user = new User3(); user.setName("张三"); session.save(user); session.getTransaction().commit(); }catch(Exception e) { e.printStackTrace(); session.getTransaction().rollback(); }finally { HibernateUtils.closeSession(session); } } public void testSave4() { Session session = null; try { session = HibernateUtils.getSession(); session.beginTransaction(); User4 user = new User4(); user.setName("张三"); session.save(user); session.getTransaction().commit(); }catch(Exception e) { e.printStackTrace(); session.getTransaction().rollback(); }finally { HibernateUtils.closeSession(session); } } 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |