【hibernate】映射可嵌入式组件

转载:https://www.cnblogs.com/yangchongxing/p/10376452.html

可嵌入 Address 类,没有对应的数据表

package cn.ycx.study.hibernate.entity;
import javax.persistence.Column;
import javax.persistence.Embeddable;
import javax.validation.constraints.NotNull;
@Embeddable
public class Address {
@NotNull
@Column(nullable=false)
protected String street;
@NotNull
@Column(nullable=false)
protected String zipcode;
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getZipcode() {
return zipcode;
}
public void setZipcode(String zipcode) {
this.zipcode = zipcode;
}
}

1、映射可嵌入组件

package cn.ycx.study.hibernate.entity;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
@org.hibernate.annotations.DynamicInsert
@org.hibernate.annotations.DynamicUpdate
public class User {
@Id
@GeneratedValue(generator="id_generator")
protected long id;
protected String username;
//嵌入式组件
protected Address address;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
}

产生的数据库表

CREATE TABLE `user` (
`id` BIGINT(20) NOT NULL,
`street` VARCHAR(255) NOT NULL,
`zipcode` VARCHAR(255) NOT NULL,
`username` VARCHAR(255) NULL DEFAULT NULL,
PRIMARY KEY (`id`)
)
COLLATE='utf8mb4_general_ci'
ENGINE=InnoDB
;

2、重写映射可嵌入组件

package cn.ycx.study.hibernate.entity;

import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
@org.hibernate.annotations.DynamicInsert
@org.hibernate.annotations.DynamicUpdate
public class User {
@Id
@GeneratedValue(generator="id_generator")
protected long id;
protected String username;
//嵌入式组件
protected Address address;
@AttributeOverrides({
@AttributeOverride(
name
= "street",
column = @Column(name = "order_street")
),
@AttributeOverride(
name = "zipcode",
column = @Column(name = "order_zipcode")
)
})
protected Address orderAddress;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public Address getOrderAddress() {
return orderAddress;
}
public void setOrderAddress(Address orderAddress) {
this.orderAddress = orderAddress;
}
}

产生的数据库表

CREATE TABLE `user` (
`id` BIGINT(20) NOT NULL,
`street` VARCHAR(255) NOT NULL,
`zipcode` VARCHAR(255) NOT NULL,
`order_street` VARCHAR(255) NULL DEFAULT NULL,
`order_zipcode` VARCHAR(255) NULL DEFAULT NULL,
`username` VARCHAR(255) NULL DEFAULT NULL,
PRIMARY KEY (`id`)
)
COLLATE='utf8mb4_general_ci'
ENGINE=InnoDB
;

3、映射嵌套的可嵌入组件

可嵌入 Address 类中嵌套 City 类

package cn.ycx.study.hibernate.entity;
import javax.persistence.Column;
import javax.persistence.Embeddable;
import javax.validation.constraints.NotNull;
@Embeddable
public class Address {
@NotNull
@Column(nullable=false)
protected String street;
@NotNull
@Column(nullable=false)
protected String zipcode;
public String getStreet() {
return street;
}
@NotNull
protected City city; public void setStreet(String street) {
this.street = street;
}
public String getZipcode() {
return zipcode;
}
public void setZipcode(String zipcode) {
this.zipcode = zipcode;
}
public City getCity() {
return city;
}
public void setCity(City city) {
this.city = city;
}
}

嵌套 City 类

package cn.ycx.study.hibernate.entity;

import javax.persistence.Column;
import javax.persistence.Embeddable;
import javax.validation.constraints.NotNull; @Embeddable
public class City {
@NotNull
@Column(nullable=false)
protected String country; @NotNull
@Column(nullable=false)
protected String name; public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

用户表

package cn.ycx.study.hibernate.entity;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
@org.hibernate.annotations.DynamicInsert
@org.hibernate.annotations.DynamicUpdate
public class User {
@Id
@GeneratedValue(generator="id_generator")
protected long id;
protected String username;
//可嵌入式组件
protected Address address;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
}

产生的数据库表

CREATE TABLE `user` (
`id` BIGINT(20) NOT NULL,
`country` VARCHAR(255) NOT NULL,
`name` VARCHAR(255) NOT NULL,
`street` VARCHAR(255) NOT NULL,
`zipcode` VARCHAR(255) NOT NULL,
`username` VARCHAR(255) NULL DEFAULT NULL,
PRIMARY KEY (`id`)
)
COLLATE='utf8mb4_general_ci'
ENGINE=InnoDB
;

 这种嵌套关系理论上是可以嵌套无数层

05-03 22:34