hibernate里面有2种类
@Entry 实体类 必须配置在hibernate.cfg.xml
mapping class=""
@embeddable嵌入式类规范 不用配置hibernate.cfg.xml
1.实现序列化接口2.重写hashcode和equals方法
场景1:联合主键 @EmbeddId
场景2:在集合映射做 复合数据类
场景1:联合主键 @EmbeddId
有共同的主键
package com.oracle.bean; import lombok.Data; import javax.persistence.Embeddable; import java.io.Serializable; @Data//已经重写了重写hashcode和equals方法 @Embeddable public class Commonpk implements Serializable{ private int sid; private int siid; }
package com.oracle.bean; import lombok.Data; import javax.persistence.EmbeddedId; import javax.persistence.Entity; import java.io.Serializable; @Data @Entity public class Mark implements Serializable{ @EmbeddedId private Commonpk pk; private String mname; }
package com.oracle.bean; import javax.persistence.EmbeddedId; import javax.persistence.Entity; import java.io.Serializable; @Entity public class Student implements Serializable{ @EmbeddedId private Commonpk pk; private String sname; }
场景2:在集合映射做 复合数据类
package com.oracle.bean; import lombok.Data; import javax.persistence.Embeddable; import java.io.Serializable; @Data @Embeddable public class RuleColor implements Serializable{ private String imgurl; private String productname; }
package com.oracle.bean; import lombok.Data; import org.hibernate.annotations.GenericGenerator; import javax.persistence.*; import java.math.BigDecimal; import java.util.List; import java.util.Map; import java.util.Set; @Data @Entity public class Product { @Id @GeneratedValue(generator = "uuid") @GenericGenerator(name = "uuid",strategy = "uuid") private String productcode;//使用uuid的主键生成策略 @ElementCollection(targetClass = RuleColor.class)
@ElementCollection(targetClass = RuleColor.class) @CollectionTable(name="t_ruleColors") @OrderBy private List<RuleColor> ruleColors; }