[@Embedded 和 @Embeddable] 指定其值为可嵌入类实例的实体的持久字段或属性。 可嵌入类必须标注为Embeddable。 @Embedded 用来修饰 对象属性(引用类型 -- 类对象 -- 属性注解) @Embeddable 用来修饰 类(类注解) 用来复用类对象的属性 可使用 @AttributeOverrides 来进行重写 如 AttributeOverride,AttributeOverrides, AssociationOverride,和AssociationOverrides 注释可以用于覆盖声明或嵌入类默认映射。 /** * @AttributeOverrides * 用于重新定义列名 * @AttributeOverride * name == 代表类的属性名 * column 代表 @Column 并重写相应的参数即可 */ @Embedded @AttributeOverrides({ @AttributeOverride(name="view1",column = @Column(name="view_1")), @AttributeOverride(name="view2",column = @Column(name="view_2",length = 100)) }) private ViewProperty viewProperty; 其中view1 和 view2 是 ViewProperty的属性
package sun.rain.amazing.javax.anno.domain.embedded; import lombok.Data; import javax.persistence.Embeddable; import java.io.Serializable; /** * @author sunRainAmazing */ @Data @Embeddable public class ViewProperty implements Serializable { private String view1; private String view2; }
package sun.rain.amazing.javax.anno.domain.embedded; import lombok.Data; import javax.persistence.Embeddable; import java.io.Serializable; /** * @author sunRainAmazing */ @Data @Embeddable public class DtoProperty implements Serializable { private String desc1; private String desc2; private ViewProperty viewProperty; }
package sun.rain.amazing.javax.anno.domain.embedded; import lombok.Data; import sun.rain.amazing.core.eums.ErrorPropertyMustCancel; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; /** * @author Reese */ @Data @Entity public class UserEmbedded { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private int id; private String name; // Persistent component using defaults private DtoProperty dtoProperty; /** * 报错 Repeated column in mapping for entity: * sun.rain.amazing.javax.anno.domain.embedded.UserEmbedded column * 因此将其注销 * */ // @ErrorPropertyMustCancel // private ViewProperty viewProperty; }
/* CREATE TABLE `user_embedded` ( `id` int(11) NOT NULL AUTO_INCREMENT, `desc1` varchar(255) DEFAULT NULL, `desc2` varchar(255) DEFAULT NULL, `view1` varchar(255) DEFAULT NULL, `view2` varchar(255) DEFAULT NULL, `name` varchar(255) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 */