本站消息

站长简介/公众号


站长简介绍:高级工程师,爱好交友,无偿辅导python和前端,技术交流,面试指导,找工作指导,瞎聊都可加我微信i88811i哈,欢迎欢迎!也欢迎加入程序员交流群,专属程序员的圈子,加我微信拉你进群.欢迎关注我的微信公众号:程序员总部,程序员的家,探索程序员的人生之路!分享IT最新技术,关注行业最新动向,让你永不落伍。了解同行们的工资,生活工作中的酸甜苦辣,谋求程序员的最终出路!

  价值13000svip视频教程,java大神匠心打造,零基础java开发工程师视频教程全套,基础+进阶+项目实战,包含课件和源码

  出租广告位,需要合作请联系站长


+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

暂无数据

Spring Data Jpa - 复合主键问题

发布于2022-05-19 07:40     阅读(805)     评论(0)     点赞(11)     收藏(5)


我使用了 play-spring-data-jpa 模板来启动一个项目并

我在尝试将休眠映射到使用复合主键的连接表时遇到问题。

我要连接的数据库是现有的 mysql 数据库。

请看下面:

SQL:

CREATE TABLE IF NOT EXISTS `text_advertising`.`campaign_item_slots` (
  `campaign_item_id` BIGINT NOT NULL,
  `advert_slot_id` BIGINT NOT NULL,
  `active` TINYINT(1) NOT NULL DEFAULT TRUE,
  `date_created` DATETIME NOT NULL,
  `date_updated` DATETIME NOT NULL,
  PRIMARY KEY (`campaign_item_id`, `advert_slot_id`),
  INDEX `fk_campaign_item_slots_2_idx` (`advert_slot_id` ASC),
  CONSTRAINT `fk_campaign_item_slots_1`
    FOREIGN KEY (`campaign_item_id`)
    REFERENCES `text_advertising`.`campaign_items` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_campaign_item_slots_2`
    FOREIGN KEY (`advert_slot_id`)
    REFERENCES `text_advertising`.`advert_slots` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_general_ci;

CampaignItemSlot 类:

@Entity
@Table(name = "campaign_item_slots")
public class CampaignItemSlot implements Serializable{
	
	@EmbeddedId
	CampaignItemSlotId campaignItemSlotId;
	
    private boolean active;
    
    private Timestamp date_created;

    private Timestamp date_updated;

}

CampaignItemSlotId 类

@Embeddable
public class CampaignItemSlotId implements Serializable{

	 @Column(name="advert_slot_id")
	 public long advertSlot;
	 
	 @Column(name="campaign_item_id")
	 public long campaignItem;
	 
}

当我设置“hibernate.hbm2ddl.auto”、“validate”时,我收到以下错误:

Caused by: org.hibernate.HibernateException: Missing column: id in text_advertising.campaign_item_slots

When I set "hibernate.hbm2ddl.auto", "update" then an id field is automatically created in the campaign_item_slots table as shown when I do a show create table. This is not what I want:

| campaign_item_slots | CREATE TABLE `campaign_item_slots` (
  `campaign_item_id` bigint(20) NOT NULL,
  `advert_slot_id` bigint(20) NOT NULL,
  `active` tinyint(1) NOT NULL DEFAULT '1',
  `date_created` datetime NOT NULL,
  `date_updated` datetime NOT NULL,
  `id` bigint(20) NOT NULL,
  `advertSlot_id` bigint(20) DEFAULT NULL,
  `campaignItem_id` bigint(20) DEFAULT NULL,
  PRIMARY KEY (`campaign_item_id`,`advert_slot_id`),
  KEY `fk_campaign_item_slots_2_idx` (`advert_slot_id`),
  KEY `FK_q87o53119u79073jwttacsqta` (`advertSlot_id`),
  KEY `FK_es60h5vj30rllkf0js1qy642y` (`campaignItem_id`),
  CONSTRAINT `FK_es60h5vj30rllkf0js1qy642y` FOREIGN KEY (`campaignItem_id`) REFERENCES `campaign_items` (`id`),
  CONSTRAINT `fk_campaign_item_slots_1` FOREIGN KEY (`campaign_item_id`) REFERENCES `campaign_items` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
  CONSTRAINT `fk_campaign_item_slots_2` FOREIGN KEY (`advert_slot_id`) REFERENCES `advert_slots` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
  CONSTRAINT `FK_q87o53119u79073jwttacsqta` FOREIGN KEY (`advertSlot_id`) REFERENCES `advert_slots` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |

This is my build.sbt:

version := "1.0-SNAPSHOT"

libraryDependencies ++= Seq(
  javaCore,
  javaJpa,  
  javaWs % "test",
  "org.springframework" % "spring-context" % "4.1.4.RELEASE",
  "org.springframework.data" % "spring-data-jpa" % "1.8.0.RELEASE",
  "org.springframework" % "spring-orm" % "4.1.4.RELEASE",
  "org.springframework" % "spring-jdbc" % "4.1.4.RELEASE",
  "org.springframework" % "spring-tx" % "4.1.4.RELEASE",
  "org.springframework" % "spring-expression" % "4.1.4.RELEASE",
  "org.springframework" % "spring-aop" % "4.1.4.RELEASE",
  "org.springframework" % "spring-test" % "4.1.4.RELEASE" % "test",	javaJpa.exclude("org.hibernate.javax.persistence", "hibernate-jpa-2.0-api"),  
  "org.hibernate" % "hibernate-entitymanager" % "4.3.8.Final",
  "javax.inject" % "javax.inject" % "1",  
  "mysql" % "mysql-connector-java" % "5.1.34",
  "org.json" % "json" % "20090211"
 )

lazy val root = (project in file(".")).enablePlugins(PlayJava)

I've tried many different combinations of using IdClass and EmbeddedId etc as described on this site: mapping variations

So whichever combination of things I do try, after some fiddling I get to the same error of:

Caused by: org.hibernate.HibernateException: Missing column: id in text_advertising.campaign_item_slots

I'm starting to wonder if it's a spring data issue.

Would really appreciate any help with this. Thanks.


解决方案


Seems like the config below did it for me:

CampaignItemSlot.java

package models;

import java.io.Serializable;
import java.sql.Timestamp;

import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinColumns;
import javax.persistence.ManyToOne;
import javax.persistence.MapsId;
import javax.persistence.Table;

@Entity
@Table(name = "campaign_item_slots")
public class CampaignItemSlot implements Serializable{
    
    @EmbeddedId
    private CampaignItemSlotPk campaignItemSlotPk;    

    @MapsId("advert_slot_id")
    @JoinColumns({
       @JoinColumn(name="advert_slot_id", referencedColumnName="id"),
    })
    @ManyToOne AdvertSlot advertSlot;
    
    @MapsId("campaign_item_id")
    @JoinColumns({
       @JoinColumn(name="campaign_item_id", referencedColumnName="id"),
    })
    @ManyToOne CampaignItem campaignItem;    
    
    private boolean active;
    
    private Timestamp date_created;

    private Timestamp date_updated;

	public CampaignItemSlot() {
		super();
		// TODO Auto-generated constructor stub
	}

	public CampaignItemSlotPk getCampaignItemSlotPk() {
		return campaignItemSlotPk;
	}

	public void setCampaignItemSlotPk(CampaignItemSlotPk campaignItemSlotPk) {
		this.campaignItemSlotPk = campaignItemSlotPk;
	}

	public boolean isActive() {
		return active;
	}

	public void setActive(boolean active) {
		this.active = active;
	}

	public Timestamp getDate_created() {
		return date_created;
	}

	public void setDate_created(Timestamp date_created) {
		this.date_created = date_created;
	}

	public Timestamp getDate_updated() {
		return date_updated;
	}

	public void setDate_updated(Timestamp date_updated) {
		this.date_updated = date_updated;
	}

	public AdvertSlot getAdvertSlot() {
		return advertSlot;
	}

	public void setAdvertSlot(AdvertSlot advertSlot) {
		this.advertSlot = advertSlot;
	}

	public CampaignItem getCampaignItem() {
		return campaignItem;
	}

	public void setCampaignItem(CampaignItem campaignItem) {
		this.campaignItem = campaignItem;
	}
	
}

CampaignItemSlotPK.java

package models;

import java.io.Serializable;
import java.sql.Timestamp;

import javax.persistence.Column;
import javax.persistence.Embeddable;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

@Embeddable
public class CampaignItemSlotPk implements Serializable{
    
    @Column(name="campaign_item_id")
    private long campaignItem;

    @Column(name="advert_slot_id")
    private long advertSlot;

	public CampaignItemSlotPk() {
		// TODO Auto-generated constructor stub
	}

	public long getCampaignItem() {
		return campaignItem;
	}

	public void setCampaignItem(long campaignItem) {
		this.campaignItem = campaignItem;
	}

	public long getAdvertSlot() {
		return advertSlot;
	}

	public void setAdvertSlot(long advertSlot) {
		this.advertSlot = advertSlot;
	}


}

build.sbt

  javaJpa.exclude("org.hibernate.javax.persistence", "hibernate-jpa-2.0-api"),
  "org.hibernate" % "hibernate-entitymanager" % "4.3.0.Final",  
  "mysql" % "mysql-connector-java" % "5.1.34",

This fixed it.

站长简介:高级工程师,爱好交友,无偿辅导python和前端,技术交流,面试指导,找工作指导,瞎聊都可加我微信i88811i哈,欢迎欢迎!也欢迎加入程序员交流群,专属程序员的圈子,加我微信拉你进群.
欢迎关注我的公众号:程序员总部,关注公众号回复java,免费领取 全套java视频教程,关注公众号回复java充值+你的账号,免费为您充值1000积分



所属网站分类: 技术文章 > 问答

作者:黑洞官方问答小能手

链接:http://www.javaheidong.com/blog/article/442847/212cd36534e547f9a770/

来源:java黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

11 0
收藏该文
已收藏

评论内容:(最多支持255个字符)