hibernate注解方式
现在很多时候大家都认为使用xml配置过于繁琐,更习惯于使用注解的方式进行配置hibernate的映射关系了,可以将注解放在属性上,也可以放在getter方法上
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
| @Entity(name = "log") public class Log { private long id; private String requestUri; private String params; private String msg; private long startDate; private long endDate;
@Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "id", unique = true, nullable = false) public long getId() { return id; }
public void setId(long id) { this.id = id; } @Column(name = "requestUri") public String getRequestUri() { return requestUri; }
public void setRequestUri(String requestUri) { this.requestUri = requestUri; } @Column(name = "params") public String getParams() { return params; }
public void setParams(String params) { this.params = params; }
@Column(name = "startDate") public long getStartDate() { return startDate; }
public void setStartDate(long startDate) { this.startDate = startDate; } @Column(name = "endDate") public long getEndDate() { return endDate; }
public void setEndDate(long endDate) { this.endDate = endDate; }
@Column(name = "msg") public String getMsg() { return msg; }
public void setMsg(String msg) { this.msg = msg; } }
|
使用注解方式的话在构建配置时由于没有了映射配置文件,需要addAnnotatedClass所对应的实体类
1 2 3
| Configuration configuration = new Configuration(); configuration.configure() .addAnnotatedClass(TestAnn.class);
|