-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Tenant_id as part of composite key in jpa
PostPosted: Fri Oct 28, 2016 9:41 pm 
Newbie

Joined: Fri Oct 28, 2016 9:03 pm
Posts: 2
Hola, Todos

Disculpenme por no escribir en Ingles y espero encontrar alguien quien me pueda ayudar con mi problema.

Estoy queriendo utilizar Hibernate para la persistencia y me veo con problema para implementar el modelo de mis tablas que tiene tenant_id como parte de clave compuesta, a modo de simplificar, detallo el diseño de mis tablas.

ESTAS SON LAS TABLAS
-------
T1
-------
t1_id (pk)
tenant_id (pk)
...


-------
T2
-------
t2_id (pk)
tenant_id (pk) (fk t1.tenant_id)
t1_id (fk t1.t1_id)
......

ESTOS SON LOS MODELOS QUE NO ME FUNCIONAN

Code:
@Entity
class T1{

   @EmbeddedId
   private T1PK pk;
   
}

@Embeddable
class T1PK {
  @Id
  private int t1_id;
 
@Id
  private int tenant_id;
 
}

@Embeddable
class T2PK {
  @Id
  private int t2_id;
  @Id
  private int tenant_id;
}

@Entity
class T2 {
  @EmbeddedId
  private T2PK pk;

  @MapsId("tenant_id")
  @ManyToOne(optional = false)
  @JoinColumns(value = {
          @JoinColumn(name = "tenant_id", referencedColumnName = "tenant_id"),
          @JoinColumn(name = "t1_id", referencedColumnName = "t1_id") })
  private T1 t1;
}


Este modelo lo que no logro persistir en la base de datos, el tema creo que esta en la tabla T2 donde el campo tenant_id es foreign key y al mismo tiempo es parte de la clave primaria compuesta . Ya busque mucha información pero no consigo hacer andar, necesito eso para aislar los datos de cada empresa a nivel de base de datos.

Espero algún comentario sobre lo que quiero implementar.

Edgar Villalba, desde PARAGUAY...


Top
 Profile  
 
 Post subject: Re: Tenant_id as part of composite key in jpa
PostPosted: Sat Oct 29, 2016 1:11 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
Although I can easily read Spanish, I don't know how to speak it, so I'll answer you in English.

First of all, why do you have @Id in Embeddables? That's not correct. Embeddables don't mark any @Id even when they are used as @EmbeddedId.

Second, because you use the whole EmbeddedId as an FK, the MapsId must not take any attribute:

Code:
@MapsId
@ManyToOne(optional = false)
@JoinColumns(value = {
@JoinColumn(name = "tenant_id", referencedColumnName = "tenant_id"),
@JoinColumn(name = "t1_id", referencedColumnName = "t1_id") })
private T1 t1;


Top
 Profile  
 
 Post subject: Tenant_id as part of composite key in jpa
PostPosted: Sat Oct 29, 2016 10:55 am 
Newbie

Joined: Fri Oct 28, 2016 9:03 pm
Posts: 2
Estimado Amigo,

El tema del @Id en la tabla @Embeddable fue por equivocación en la transcripción de mis modelos, el punto aquí es que T1 Y T2 tienen campo compuesto y compartan el tenant_id como parte de primary key de t2 y al mismo tiempo es FK de t1. Veo que es imposible usar @MapsId solo porque solo tenant_id de T1 está involucrado en el primary key de T2.

Este seria el script de mi base de datos que quiero modelar en HIBERNATE.


CREATE TABLE `T1` (
`t1_id` int(11) NOT NULL,
`tenant_id` int(11) NOT NULL,
`description_t1` varchar(45) NOT NULL,
PRIMARY KEY (`t1_id`,`tenant_id`),
UNIQUE KEY `t1_id_UNIQUE` (`t1_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `T2` (
`t2_id` int(11) NOT NULL,
`tenant_id` int(11) NOT NULL,
`t1_id` int(11) NOT NULL,
`description_t2` varchar(45) DEFAULT NULL,
PRIMARY KEY (`t2_id`,`tenant_id`),
UNIQUE KEY `t2_id_UNIQUE` (`t2_id`),
KEY `fk_t2_t1Id_idx` (`t1_id`,`tenant_id`),
CONSTRAINT `fk_t2_t1Id` FOREIGN KEY (`t1_id`, `tenant_id`) REFERENCES `T1` (`t1_id`, `tenant_id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8;


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.