hibernate中的嵌入类2种使用方法

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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值