hibernate mapping configuration Annotation mode

import java.util.HashSet;
import java.util.Set;
import javax.persistence.*;
import org.hibernate.annotations.GenericGenerator;

 

@Entity
@Table(name = "department", catalog = "db4myeclipse")
public class Department implements java.io.Serializable {

	private static final long serialVersionUID = 1L;
	private String id;
	private String deptName;
	private Department parentDept;
	private Set<User> users = new HashSet<User>(0);
	private Set<Department> childDepts = new HashSet<Department>(0);

	public Department() {
	}
	public Department(String id, String deptName) {
		this.id = id;
		this.deptName = deptName;
	}

	@GenericGenerator(name = "idGenerator", strategy = "assigned")
	@Id
	@GeneratedValue(generator = "idGenerator")
	@Column(name = "id", unique = true, nullable = false)
	public String getId() {
		return this.id;
	}

	public void setId(String id) {
		this.id = id;
	}

	@ManyToOne
	@JoinColumn(name = "parent_id")
	public Department getParentDept() {
		return this.parentDept;
	}

	public void setParentDept(Department parentDept) {
		this.parentDept = parentDept;
	}

	@Column(name = "deptName", nullable = false, length = 32)
	public String getDeptName() {
		return this.deptName;
	}

	public void setDeptName(String deptName) {
		this.deptName = deptName;
	}

	@OneToMany(mappedBy = "department")
	public Set<User> getUsers() {
		return this.users;
	}

	public void setUsers(Set<User> users) {
		this.users = users;
	}

	@OneToMany(mappedBy = "parentDept")
	public Set<Department> getChildDepts() {
		return this.childDepts;
	}

	public void setChildDepts(Set<Department> childDepts) {
		this.childDepts = childDepts;
	}

}

 

@Entity
@Table(name = "idcard", catalog = "db4myeclipse")
public class IdCard implements java.io.Serializable {

	private static final long serialVersionUID = 1L;
	private String id;
	private String cardNo;
	private Timestamp authDate;
	private User user;
	public IdCard() {
	}

	public IdCard(String cardNo, Timestamp authDate, User user) {
		this.cardNo = cardNo;
		this.authDate = authDate;
		this.user = user;
	}

	@GenericGenerator(name = "idGenerator", strategy = "uuid")
	@Id
	@GeneratedValue(generator = "idGenerator")
	@Column(name = "id", unique = true, nullable = false)
	public String getId() {
		return this.id;
	}

	public void setId(String id) {
		this.id = id;
	}

	@Column(name = "cardNo", nullable = false, length = 32)
	public String getCardNo() {
		return this.cardNo;
	}

	public void setCardNo(String cardNo) {
		this.cardNo = cardNo;
	}

	@Column(name = "authDate", nullable = false)
	public Timestamp getAuthDate() {
		return this.authDate;
	}

	public void setAuthDate(Timestamp authDate) {
		this.authDate = authDate;
	}
	
	@OneToOne(mappedBy = "idCard")
	public User getUser() {
		return this.user;
	}

	public void setUser(User user) {
		this.user = user;
	}
}

 

@Entity
@Table(name = "role", catalog = "db4myeclipse")
public class Role implements java.io.Serializable {

	private static final long serialVersionUID = 1L;
	private Integer id;
	private String roleName;
	private String roleDesc;
	private Set<User> users = new HashSet<User>(0);
	public Role() {
	}

	public Role(String roleName) {
		this.roleName = roleName;
	}
	public Role(String roleName, String roleDesc, Set<User> users) {
		this.roleName = roleName;
		this.roleDesc = roleDesc;
		this.users = users;
	}

	@GenericGenerator(name = "idGenerator", strategy = "native")
	@Id
	@GeneratedValue(generator = "idGenerator")
	@Column(name = "id", unique = true, nullable = false)
	public Integer getId() {
		return this.id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	@Column(name = "roleName", nullable = false, length = 32)
	public String getRoleName () {
		return this.roleName;
	}

	public void setRoleName(String roleName) {
		this.roleName = roleName;
	}

	@Column(name = "roleDesc", length = 128)
	public String getRoleDesc () {
		return this.roleDesc;
	}

	public void setRoleDesc(String roleDesc) {
		this.roleDesc = roleDesc;
	}

	@ManyToMany (
			targetEntity = com.jaeson.hibernatestudy.bean.User.class,
			mappedBy = "roles")
	public Set<User> getUsers() {
		return this.users;
	}

	public void setUsers(Set<User> users) {
		this.users = users;
	}
 
}

 

@Entity
@Table(name = "user", catalog = "db4myeclipse")
public class User implements java.io.Serializable {

	private static final long serialVersionUID = 1L;
	private String id;
	private IdCard idCard;
	private Department department;
	private String userName;
	private Address address;
	private Set<Role> roles = new HashSet<Role>(0);
	public User() {
	}

	public User(String userName, Address address) {
		this.userName = userName;
		this.address = address;
	}

	@GenericGenerator(name = "idGenerator", strategy = "uuid")
	@Id
	@GeneratedValue(generator = "idGenerator")
	@Column(name = "id", unique = true, nullable = false)
	public String getId() {
		return this.id;
	}

	public void setId(String id) {
		this.id = id;
	}

	@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
	@JoinColumn(name="card_id", unique=true)
	public IdCard getIdCard() {
		return this.idCard;
	}

	public void setIdCard(IdCard idCard) {
		this.idCard = idCard;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "dept_id")
	public Department getDepartment() {
		return this.department;
	}

	public void setDepartment(Department department) {
		this.department = department;
	}

	@Column(name = "userName", nullable = false, length = 32)
	public String getUserName() {
		return this.userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	@Embedded
	@AttributeOverrides({
		@AttributeOverride(name="address",	column = @Column(name="address") ),
		@AttributeOverride(name="zipCode",	column = @Column(name="zipCode") ),
		@AttributeOverride(name="phone", 	column = @Column(name="phone") ),
		})
	public Address getAddress() {
		return this.address;
	}

	public void setAddress(Address address) {
		this.address = address;
	}
 
	@ManyToMany (
			targetEntity=com.jaeson.hibernatestudy.bean.Role.class,
			cascade=CascadeType.ALL)
	@JoinTable(
			name="user_role",
			joinColumns=@JoinColumn(name="user_id"),
			inverseJoinColumns=@JoinColumn(name="role_id")
			)
	public Set <Role> getRoles () {
		return this.roles;
	}

	public void setRoles(Set<Role> roles) {
		this.roles = roles;
	}
}
@Embeddable
public class Address implements Serializable {
 
	private static final long serialVersionUID = 1L;
	private String address;
	private Integer zipCode;
	private String phone;
 
	public Address() {}
 
	public Address(String address, Integer zipCode, String phone) {
		this.address = address;
		this.zipCode = zipCode;
		this.phone = phone;
	}
 
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public Integer getZipCode() {
		return zipCode;
	}
	public void setZipCode(Integer zipCode) {
 		this.zipCode = zipCode;
	}
	public String getPhone() {
 		return phone;
	}
	public void setPhone(String phone) {
 		this.phone = phone;
	}
}

 

 * By default declarations in hbm files have higher priority than annotation metadata in classes.

 

 * hibernate uses persistence annotations defined by javax.persistence.*


 * @Entity annotation declares a class as an entity bean (ie, a persistent POJO class)
 * @Id annotation declares the identity property of the entity bean.
 * Other mapping definitions are implicit. This implicit mapping As the main body, the configuration method with explicit mapping as the exception is in a very difficult position in the new EJ3 specification.

   Important location.
 * When annotating a class, you can choose to annotate its properties or methods, depending on your choice, Hibernate's access type

   Field or property, respectively.

 * @Table is a class-level annotation. The @Table annotation can specify table, catalog, and schema for entity bean mapping.

   Name.
   If @Table is not defined, the system automatically uses the default value: the short class name of the entity (without the package name). The
   @Table element includes a schema and a catalog attribute, which can be specified if necessary. 
 
 * @Id An annotation can define a property in an entity bean as an identifier. The value of this property can be set by the application itself,

   It can also be generated by Hiberante (recommended).
   Use the @GeneratedValue annotation to define the generation strategy of the identifier: 
    strategy specifies the generation strategy, the default is GenerationType . AUTO
    generator specifies the generator used to generate the primary key.
    AUTO - can be of identity column type, Either sequence type or table type, depending on the underlying database.
    • TABLE - use table to store id values 
    ​​• IDENTITY - primary key is automatically generated by the database, mainly auto-increment type
    • SEQUENCE - sequence
   @org.hibernate.annotations.GenericGenerator allows you Define a Hibernate specific id generator.
   @GenericGenerator(name="generator", strategy = "uuid")
   @Id
   @GeneratedValue(generator="generator")
   @Column(name = "id", unique = true, nullable = false)

 * @Basic All properties that do not have annotations defined are equivalent to adding @Basic annotations on them. The @Basic annotations can declare properties of

   Fetch strategy (fetch strategy), FetchType.LAZY or FetchType.EAGER.
 
 * @Column annotation can map properties to columns.
   @Column(
        name="columnName";
        boolean unique() default false; 
        boolean nullable() default true;
        int length() default 255)

 * Component mapping:
    The component class must define the @Embeddable annotation at the class level. Use @Embedded and 

    The @AttributeOverride annotation can override the column mapping of the embedded object corresponding to the attribute:
    @Embedded
    @AttributeOverrides( {
        @AttributeOverride(name="address", column = @Column(name="address") ),
        @AttributeOverride(name=" zipCode", column = @Column(name="zipCode") ),
        @AttributeOverride(name="phone", column = @Column(name="phone") ),
    })

 * cascade: cascade, it can have Five values ​​are optional, they are:
    CascadeType.PERSIST: Cascade new

    CascadeType.MERGE : Cascade update
    CascadeType.REMOVE : Cascade delete
    CascadeType.REFRESH : Cascade refresh

    CascadeType.ALL: All four of the above

 * @OneToOne unique foreign key association: a one-to-one association may be bidirectional. In a bidirectional association, one and only one end is the owner end

    Existing: The principal side is responsible for maintaining the join column (ie update). 
    For the slave table that does not need to maintain this relationship, it is declared through the mappedBy attribute. The value of mappedBy points to the associated attribute of the principal. 
    Annotate five attributes: targetEntity, cascade, fetch , optional and mappedBy, the
    default value of the fetch property is FetchType.EAGER.
    optional = true sets the foreign key property can be null
    targetEntity property: property of type Class. Define the type of the relationship class, the default is the class type
    cascade attribute corresponding to the member attribute: CascadeType[] type.

    Foreign key side:

    @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @JoinColumn(name="card_id", unique=true)
    主键端:

    @OneToOne(mappedBy = "idCard")

 * @Many-to-one: @JoinColumn is optional, the default value of the associated field is: the associated property name of the main body + underscore + the associated end

    Primary key column name. The 
    default value of the fetch property is FetchType.EAGER.
    Multiport:

    @ManyToOne(fetch =
    FetchType.LAZY) @JoinColumn(name="dept_id")

 * @One-to-many: in the EJB3 specification the many-to-one side is almost always the owner side of a bidirectional association, and a To-many side association

     Annotated as:

     @OneToMany(mappedBy="department") The
     default value of the fetch property is FetchType.LAZY.


 * @Many-to-many: Definable many-to-many association via @ManyToMany annotation. At the same time, you also need to annotate @JoinTable

     Describe the association table and association conditions. If it is a two-way association, one segment must be defined as owner, and the other end must be defined as inverse . The
     default value of the fetch attribute is FetchType.LAZY.
     @ManyToMany(
          targetEntity=com.jaeson.hibernatestudy.bean.Role.class,
          cascade={CascadeType.PERSIST, CascadeType.MERGE})
     @JoinTable(
          name="user_role",
          joinColumns=@JoinColumn(name="user_id "),
          inverseJoinColumns=@JoinColumn(name="role_id "))
    

     @ManyToMany(
          mappedBy = "roles",
          targetEntity = com.jaeson.hibernatestudy.bean.User.class)

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326989016&siteId=291194637