Hibernate数据库映射 [英] Hibernate database mapping

查看:205
本文介绍了Hibernate数据库映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我对Hibernate有点新鲜。要了解我在一个项目中使用它的技术。我尝试映射以下数据库:

 广告系列
campaignId(+)
名称

促销
campaignId(+)
折扣(+)
产品
消息

我在两种情况下都用(+)表示了主键。 Promotion中的'campaignId'是Campaign建立1:m映射模型的外键(一个Campaign有许多促销活动)。使用注释我坚持如何做到这一点。



我并不想在Promotion表中添加promotionId,因为它处理数据繁琐。这当然会使桥接表有点棘手。我也遇到了使用外键的问题,外键也是主键的一部分。



映射是否可能?






好的,我得到了它的工作。有点。必须检查持久性是否真的有效。我做了以下工作:

  @Entity 
@Table(name =CAMPAIGNS)
@Audited
public class CampaignEntity {
private int campaignId;
私人字符串名称;
私人列表< PromotionEntity>促销;

public CampaignEntity(int campaignId,String name){
this.campaignId = campaignId;
this.name = name;

$ b @Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name =cmp_id)
public int getCampaignId() {
return campaignId;
}

public void setCampaignId(int campaignId){
this.campaignId = campaignId;
}

//此处的广告系列名称...省略以节省空间

@OneToMany
@JoinColumn(name =cmp_id)
公开列表< PromotionEntity> getPromotions(){
退货促销;
}

public void setPromotions(List< PromotionEntity> promotions){
this.promotions = promotions;






促销是一种香草映射(根本不使用嵌入式),字段为:campaignId,折扣,消息。 (它也没有@ManyToOne批注。)



是否有意义?



最后,这将是一等奖:你可以看到我正在使用Envers来审计整个事情。上面创建了一个相当丑陋的CampaignEntity_PromotionEntity_AUD表。我知道这是必要的,但我怎么能将它重命名为CAMPAIGN_PROMOTION_AUD?

谢谢你们!






我在一个孤独的网站上得到了一个答案,这个网站深深地隐藏在Hibernate的Jira错误跟踪网站的远端: https://hibernate.onjira.com/browse/HHH-3729

答案是当然使用@AuditJoinTable(name =CAMPAIGN_PROMOTION_AUD)。

解决方案

这就是我现在使用的。它运作良好,Hibernate为我处理促销活动的PK。

  @Entity 
@Table(name =CAMPAIGNS)
@Audited
public class CampaignEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name =id,nullable = false)
私人整数campaignId;

@Column(name =name,nullable = false,unique = true)
private String campaignName;

@OneToMany(cascade = CascadeType.ALL,orphanRemoval = true)
@JoinTable(name =CAMPAIGN_PROMOTIONS,
joinColumns = {@JoinColumn(name =campaign_id)) },
inverseJoinColumns = {@JoinColumn(name =promotion_id)})
private设置< PromotionEntity>促销;

...
}

然后,PromotionEntity:

  @Entity 
@Table(name =PROMOTIONS)
@Audited
public类PromotionEntity实现了Comparable< PromotionEntity> {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;

@Column(name =discount,nullable = false)
私人整数折扣;

@Column(name =message,nullable = false)
private String message;

...
}

我也喜欢注释而不是getter,因为它更紧凑,读起来更容易。


Firstly, I am somewhat new with Hibernate. To get to know the technology I am using it in a project. I am trying to map the following database:

Campaign
  campaignId(+)
  name

Promotion
  campaignId(+)
  discount(+)
  product
  message

I've indicated the primary key in both cases with a (+). The 'campaignId' in Promotion is a foreign key to Campaign to model the 1:m mapping (A Campaign has many Promotions). Using annotations I am stuck on how to do this.

I do not really want to add a promotionId in the Promotion table as it makes working with the data cumbersome. This of course, makes the bridging table a bit tricky. I also have problems working with a foreign key that is also part of the primary key.

Is a mapping for this possible at all?


Ok, I got it working. Sort of. Have to check if persistence actually work. I did the following:

@Entity
@Table(name = "CAMPAIGNS")
@Audited
public class CampaignEntity {
    private int campaignId;
    private String name;
    private List<PromotionEntity> promotions;

    public CampaignEntity(int campaignId, String name) {
        this.campaignId = campaignId;
        this.name = name;
    }

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "cmp_id")
    public int getCampaignId() {
        return campaignId;
    }

    public void setCampaignId(int campaignId) {
        this.campaignId = campaignId;
    }

    // Campaign name here... left out to save space

    @OneToMany
    @JoinColumn(name = "cmp_id")
    public List<PromotionEntity> getPromotions() {
        return promotions;
    }

    public void setPromotions(List<PromotionEntity> promotions) {
        this.promotions = promotions;
    }
}

Promotion is a vanilla mapping (not using embedded after all), with the fields: campaignId, discount, message. (It also does not have a @ManyToOne annotation.)

Does that make sense?

Lastly, and this will be first prize: as you can see I'm using Envers to audit the whole thing. The above creates a rather ugly "CampaignEntity_PromotionEntity_AUD" table. I understand that it is needed, but how can I rename it to CAMPAIGN_PROMOTION_AUD rather?

Thanks guys!


I got an answer on a lonely website deeply hidden away in far-corners of the Hibernate's Jira error tracking website: https://hibernate.onjira.com/browse/HHH-3729.

The answer is to use @AuditJoinTable(name = "CAMPAIGN_PROMOTION_AUD") of course.

解决方案

This is what I am now using. It works well and Hibernate handles the PKs of the Promotions for me. Thanks again.

@Entity
@Table(name = "CAMPAIGNS")
@Audited
public class CampaignEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", nullable = false)
    private Integer campaignId;

    @Column(name = "name", nullable = false, unique = true)
    private String campaignName;

     @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
     @JoinTable(name = "CAMPAIGN_PROMOTIONS", 
                joinColumns = { @JoinColumn(name = "campaign_id") }, 
                inverseJoinColumns = { @JoinColumn(name = "promotion_id") })
     private Set<PromotionEntity> promotions;

     ...
}

and then, PromotionEntity:

@Entity
@Table(name = "PROMOTIONS")
@Audited
public class PromotionEntity implements Comparable<PromotionEntity> {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    @Column(name = "discount", nullable = false)
    private Integer discount;

    @Column(name = "message", nullable = false)
    private String message;

    ...
}

I also prefer annotating the fields rather than the getters as it is more compact and reads easier.

这篇关于Hibernate数据库映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆