Hibernate Annotation使用实例

jar说明 hibernate 3.3.0 hibernate-annotations(3.4.0.CR2)

数据库(oracle)结构

CREATE TABLE KANG_USER
(
ID VARCHAR2(32 BYTE) NOT NULL,
USERNAME VARCHAR2(30 BYTE),
PASSWORD VARCHAR2(32 BYTE),
CITY VARCHAR2(32 BYTE),
ADDTIME DATE
)

CREATE TABLE KANG_CITY
(
ID VARCHAR2(32 BYTE),
CITYNAME VARCHAR2(20 BYTE)
)

一个用户表,一个城市表,用户和城市是多对一的关系

Java代码

1. import java.util.Date;
2.
3. import javax.persistence.Entity;
4. import javax.persistence.GeneratedValue;
5. import javax.persistence.Id;
6. import javax.persistence.JoinColumn;
7. import javax.persistence.ManyToOne;
8. import javax.persistence.Table;
9. import javax.persistence.Temporal;
10. import javax.persistence.TemporalType;
11.
12. import org.hibernate.annotations.Cache;
13. import org.hibernate.annotations.CacheConcurrencyStrategy;
14. import org.hibernate.annotations.Cascade;
15. import org.hibernate.annotations.CascadeType;
16. import org.hibernate.annotations.GenericGenerator;
17. import org.hibernate.annotations.NotFound;
18. import org.hibernate.annotations.NotFoundAction;
19.
20. /**
21. * @author yukang
22. *
23. */
24. @Entity
25. @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
26. @Table(name="KANG_USER")
27. public class User {
28.
29. @Id
30. @GeneratedValue(generator = "system-uuid")
31. @GenericGenerator(name = "system-uuid", strategy = "uuid")
32. private String id;
33.
34. private String userName;
35.
36. private String passWord;
37.
38. @Temporal(TemporalType.TIMESTAMP)
39. private Date addTime;
40.
41. @ManyToOne()
42. @Cascade(value={CascadeType.PERSIST, CascadeType.MERGE})
43. @JoinColumn(name="CITY")
44. @NotFound(action=NotFoundAction.IGNORE)
45. private City city;
46.
47. public User() {
48.
49. }
50.
51.
52. /**
53. * @return
54. */
55. public String getId() {
56. return id;
57. }
58.
59. /**
60. * @param id the id to set
61. */
62. public void setId(String id) {
63. this.id = id;
64. }
65.
66. /**
67. * @return the userName
68. */
69. public String getUserName() {
70. return userName;
71. }
72.
73.
74. /**
75. * @param userName the userName to set
76. */
77. public void setUserName(String userName) {
78. this.userName = userName;
79. }
80.
81.
82. /**
83. * @return the passWord
84. */
85. public String getPassWord() {
86. return passWord;
87. }
88.
89.
90. /**
91. * @param passWord the passWord to set
92. */
93. public void setPassWord(String passWord) {
94. this.passWord = passWord;
95. }
96.
97. /**
98. * @return
99. */
100. public Date getAddTime() {
101. return addTime;
102. }
103.
104. /**
105. * @param addTime the addTime to set
106. */
107. public void setAddTime(Date addTime) {
108. this.addTime = addTime;
109. }
110.
111. /**
112. * @return
113. */
114. public City getCity() {
115. return city;
116. }
117.
118. /**
119. * @param city the city to set
120. */
121. public void setCity(City city) {
122. this.city = city;
123. }
124.
125.
126.
127. }

import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;

/**
* @author yukang
*
*/
@Entity
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@Table(name="KANG_USER")
public class User {

@Id
@GeneratedValue(generator = "system-uuid")
@GenericGenerator(name = "system-uuid", strategy = "uuid")
private String id;

private String userName;

private String passWord;

@Temporal(TemporalType.TIMESTAMP)
private Date addTime;

@ManyToOne()
@Cascade(value={CascadeType.PERSIST, CascadeType.MERGE})
@JoinColumn(name="CITY")
@NotFound(action=NotFoundAction.IGNORE)
private City city;

public User() {

}


/**
* @return
*/
public String getId() {
return id;
}

/**
* @param id the id to set
*/
public void setId(String id) {
this.id = id;
}

/**
* @return the userName
*/
public String getUserName() {
return userName;
}


/**
* @param userName the userName to set
*/
public void setUserName(String userName) {
this.userName = userName;
}


/**
* @return the passWord
*/
public String getPassWord() {
return passWord;
}


/**
* @param passWord the passWord to set
*/
public void setPassWord(String passWord) {
this.passWord = passWord;
}

/**
* @return
*/
public Date getAddTime() {
return addTime;
}

/**
* @param addTime the addTime to set
*/
public void setAddTime(Date addTime) {
this.addTime = addTime;
}

/**
* @return
*/
public City getCity() {
return city;
}

/**
* @param city the city to set
*/
public void setCity(City city) {
this.city = city;
}



}


Java代码

1. import java.util.HashSet;
2. import java.util.Set;
3.
4. import javax.persistence.Entity;
5. import javax.persistence.FetchType;
6. import javax.persistence.GeneratedValue;
7. import javax.persistence.Id;
8. import javax.persistence.OneToMany;
9. import javax.persistence.Table;
10.
11. import org.hibernate.annotations.Cache;
12. import org.hibernate.annotations.CacheConcurrencyStrategy;
13. import org.hibernate.annotations.Cascade;
14. import org.hibernate.annotations.CascadeType;
15. import org.hibernate.annotations.GenericGenerator;
16.
17. /**
18. * @author yukang
19. *
20. */
21.
22. @Entity
23. @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
24. @Table(name="KANG_CITY")
25. public class City {
26.
27. @Id
28. @GeneratedValue(generator = "system-uuid")
29. @GenericGenerator(name = "system-uuid", strategy = "uuid")
30. private String id;
31.
32. private String cityName;
33.
34. @OneToMany(mappedBy="city",fetch=FetchType.EAGER)
35. @Cascade(value={CascadeType.DELETE_ORPHAN})//级联删除
36. private Set<User> userSet;
37.
38. public City() {
39.
40. }
41.
42. /**
43. * @return the id
44. */
45. public String getId() {
46. return id;
47. }
48.
49. /**
50. * @param id the id to set
51. */
52. public void setId(String id) {
53. this.id = id;
54. }
55.
56. /**
57. * @return the cityName
58. */
59. public String getCityName() {
60. return cityName;
61. }
62.
63. /**
64. * @param cityName the cityName to set
65. */
66. public void setCityName(String cityName) {
67. this.cityName = cityName;
68. }
69.
70. /**
71. * @return the userSet
72. */
73. public Set<User> getUserSet() {
74. return userSet;
75. }
76.
77. public void addUser(User user) {
78. if ( userSet == null ) {
79. userSet = new HashSet<User>();
80. }
81. user.setCity(this);
82. userSet.add( user );
83. }
84.
85. }

import java.util.HashSet;
import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
import org.hibernate.annotations.GenericGenerator;

/**
* @author yukang
*
*/

@Entity
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@Table(name="KANG_CITY")
public class City {

@Id
@GeneratedValue(generator = "system-uuid")
@GenericGenerator(name = "system-uuid", strategy = "uuid")
private String id;

private String cityName;

@OneToMany(mappedBy="city",fetch=FetchType.EAGER)
@Cascade(value={CascadeType.DELETE_ORPHAN})//级联删除
private Set<User> userSet;

public City() {

}

/**
* @return the id
*/
public String getId() {
return id;
}

/**
* @param id the id to set
*/
public void setId(String id) {
this.id = id;
}

/**
* @return the cityName
*/
public String getCityName() {
return cityName;
}

/**
* @param cityName the cityName to set
*/
public void setCityName(String cityName) {
this.cityName = cityName;
}

/**
* @return the userSet
*/
public Set<User> getUserSet() {
return userSet;
}

public void addUser(User user) {
if ( userSet == null ) {
userSet = new HashSet<User>();
}
user.setCity(this);
userSet.add( user );
}

}

Spring中的sessionFactory的class要更改为org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean这个class

hibernate.cfg.xml要把User和City这两个class加上去

<mapping class="com.xxxx.domain.pojo.User"/>
<mapping class="com.xxxx.domain.pojo.City"/>

其他的地方以前怎么写的,现在不要改动,例子较简单,代码不做说明了
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值