- 注意import javax.xx.Entity ,而不是org.hibernate.xx.Entity。
- Descn属性不存在于数据库中,用@Transient 注明
- ------------------------------------------
- 1,需要: Hibernate库文件,Hibernate Annotations库,ejb3-persstence.jar(Java 持久化API)
- sessionFactory=new AnnotationConfiguration().buildSessionFactory();
- ------------------------------------------
- 2,<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
- <property name="annotatedClasses">
- <list>
- <value>com.onjava.modelplanes.domain.PlaneType</value>
- </list>
- </property>
- </bean>
- ------------------------------------------
- 1,@Entity
- @Table(name = "teacher_info")
- @IdClass(UUIDHexGenerator.class)
- public class UserMember implements java.io.Serializable
- 2,@entity通过getters/setters方法访问,或直接访问他的成员变量。
- @Entity(access = AccessType.PROPERTY)
- @Entity(access = AccessType.FIELD)
- ------------------------------------------
- 映射标识符
- 1,@Id
- @GeneratedValue(strategy = GenerationType.AUTO)
- private String id;
- 2,@Id(generate=GeneratorType.SEQUENCE, generator='SEQ_STORE')
- 3,@Id(generate=GeneratorType.IDENTITY)
- ------------------------------------------
- 映射属性
- 1,@Transient
- 2,@Column(name="PLANE_ID", length=80, nullable=true)
- 3,@Basic(fetch = FetchType.LAZY)
- 4,@Serialized 凡标识@Serialized的属性将被序列化
- public Country getCountry() { ... }
- 5,@Lob标识了存储对象可能是个CLOB或者BLOB。
- @Lob(type=LobType.CLOB)
- public String getFullText(){return fullText;}
- @Lob(type = LobType.BLOB)
- public byte[] getFullCode() {return fullCode;}
- @Version 定义乐观锁机制使用
- ------------------------------------------
- 关联关系:
- 一、一对一:
- 1,@OneToOne(mappedBy = "address")
- public User getUser() {
- return user;
- }
- 1、两边都定义了@OneToOne,但都没有定义mappedBy,则user和address表都会生成到对方的外键,双方都是这个关系的拥有者。
- 2、两边都定义了@OneToOne,如果user定义了mappedBy,则在address表生成到user的外键,address是这个关系的拥有者;如果address定义
- 了mappedBy,则在user表生成到address的外键,user是这个关系的拥有者。
- 二、一对多,多对一:
- 2,@ManyToOne( cascade = {CascadeType.PERSIST, CascadeType.MERGE} )
- 3,@OneToMany(mappedBy="planeType",cascade=CascadeType.ALL, fetch=FetchType.EAGER)
- @OrderBy("name")
- public List<ModelPlane> getModelPlanes() {
- return modelPlanes;
- }
- 其中定义mappedBy的是@OneToMany,也就是说One这一方是关系的拥有者。Many一方的表中生成到关联类的外键。
- 三、@ManyToMany
- private Set authors = new HashSet<Author>();
- @ManyToMany
- public Set<Author> getAuthors(){
- return authors;
- }
- private Set books = new HashSet<Book>();
- @ManyToMany(mappedBy="authors")
- public Set<Book> getBooks(){
- return books;
- }
- @ManyToMany会生成中间表,具体表名和字段可以通过@AssociationTable来定义,默认的就可以了,同样关系的非拥有者,需要定义mappedBy属性。
- ------------------------------------------
- 命名查询
- 你也可以通过注解,利用@NameQueries和@NameQuery注解,如下:
- @NamedQueries(
- {
- @NamedQuery(name="planeType.findAll",query="select p from PlaneType p" ),
- @NamedQuery(name="planeType.delete",query="delete from PlaneType where id=:id" )
- }
- )
- ------------------------------------------
- 内嵌对象(组件)
- @Embedded({
- @AttributeOverride(name='iso2', column = @Column(name='bornIso2') ),
- @AttributeOverride(name='name', column = @Column(name='bornCountryName') )
- })
- Country bornIn;
- ...
- }
- @Embeddable(access = AccessType.FIELD)
- public class Address implements Serializable {
- String city;
- Country nationality;
- }
- @Embeddable
- public class Country implements Serializable {
- private String iso2;
- private String name;
- public String getIso2() { return iso2; }
- public void setIso2(String iso2) { this.iso2 = iso2; }
- @Column(name='countryName')
- public String getName() { return name; }
- public void setName(String name) { this.name = name; }
- ...
- }
- ------------------------------------------
- 自定义的主键生成策略
- @javax.persistence.GeneratedIdTable(
- name='GEN_TABLE',
- table = @Table(name='GENERATOR_TABLE'),
- pkColumnName = 'key',
- valueColumnName = 'hi'
- )
- @javax.persistence.TableGenerator(
- name='EMP_GEN',
- tableName='GEN_TABLE',
- pkColumnValue='EMP',
- allocationSize=20
- )
- @javax.persistence.SequenceGenerator(
- name='SEQ_GEN',
- sequenceName='my_sequence'
- )
- package org.hibernate.test.metadata;
Hibernate Annotations笔记
最新推荐文章于 2022-09-05 00:29:31 发布