-->
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: Help mapping table relationships
PostPosted: Wed Jun 11, 2014 9:56 pm 
Newbie

Joined: Wed Jun 11, 2014 9:41 pm
Posts: 3
Hello, i'm new in hibernate development so i'm sorry if this is too easy for you.

First of all these are my tables:

Code:
-- -----------------------------------------------------
-- Table `sistemaFacturacion`.`codigoPostal`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `sistemaFacturacion`.`codigoPostal` (
  `codigoPostal` INT NOT NULL,
  `provincia` VARCHAR(25) NOT NULL,
  `localidad` VARCHAR(25) NOT NULL,
  `caracteristicaTel` VARCHAR(10) NOT NULL,
  PRIMARY KEY (`codigoPostal`))
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `sistemaFacturacion`.`persona`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `sistemaFacturacion`.`persona` (
  `dni` VARCHAR(10) NOT NULL,
  `nombre` VARCHAR(30) NOT NULL,
  `apellido` VARCHAR(30) NOT NULL,
  `codigoPostal` INT NOT NULL,
  `direccion` VARCHAR(45) NOT NULL,
  `telefonoFijo` VARCHAR(20) NULL,
  `telefonoCel` VARCHAR(25) NULL,
  `correo` VARCHAR(100) NULL,
  PRIMARY KEY (`dni`),
  INDEX `personaCodPostal_idx` (`codigoPostal` ASC),
  CONSTRAINT `personaCliente`
    FOREIGN KEY (`dni`)
    REFERENCES `sistemaFacturacion`.`cliente` (`dni`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `personaCodPostal`
    FOREIGN KEY (`codigoPostal`)
    REFERENCES `sistemaFacturacion`.`codigoPostal` (`codigoPostal`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


and my java code looks like this:
Code:
@Entity
@Table(name = "persona")
@Inheritance(strategy=InheritanceType.JOINED)
public abstract class Persona implements Serializable {
   
   
    private String dni;
    private String nombre;
    private String apellido;
    private int codPostal;
    private String direccion;
    private String telelfonoFijo;
    private String telefonoCel;
    private String correo;

    @Id @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name = "dni", unique = true, nullable = false, length = 10)
    public String getDni() {
        return dni;
    }
    public void setDni(String dni) {
        this.dni = dni;
    }
    @Column(name = "nombre", length = 30, nullable = false)
    public String getNombre() {
        return nombre;
    }
    public void setNombre(String nombre) {
        this.nombre = nombre;
    }
    @Column(name = "apellido", length = 30, nullable = false)
    public String getApellido() {
        return apellido;
    }
    public void setApellido(String apellido) {
        this.apellido = apellido;
    }
    @Column(name = "codigoPostal", nullable = false)
    public int getCodPostal() {
        return codPostal;
    }
    public void setCodPostal(int codPostal) {
        this.codPostal = codPostal;
    }
    @Column(name = "direccion", length = 45, nullable = false)
    public String getDireccion() {
        return direccion;
    }
    public void setDireccion(String direccion) {
        this.direccion = direccion;
    }
    @Column(name = "telefonoFijo", length = 20)
    public String getTelelfonoFijo() {
        return telelfonoFijo;
    }
    public void setTelelfonoFijo(String telelfonoFijo) {
        this.telelfonoFijo = telelfonoFijo;
    }
    @Column(name = "telefonoCel", length = 25)
    public String getTelefonoCel() {
        return telefonoCel;
    }
    public void setTelefonoCel(String telefonoCel) {
        this.telefonoCel = telefonoCel;
    }
    @Column(name = "correo", length = 100)
    public String getCorreo() {
        return correo;
    }
    public void setCorreo(String correo) {
        this.correo = correo;
    }


how do i have to do if i want to indicate that the attribute "codigoPostal" is a FK to another table without having to create another class named CodigoPostal.

Hope i was clear and thanks in advance!


Top
 Profile  
 
 Post subject: Re: Help mapping table relationships
PostPosted: Thu Jun 12, 2014 5:01 am 
Hibernate Team
Hibernate Team

Joined: Fri Sep 09, 2011 3:18 am
Posts: 295
Quote:
how do i have to do if i want to indicate that the attribute "codigoPostal" is a FK to another table without having to create another class named CodigoPostal.


I don't think you can, but looking at your db it would make sense to have an entity for CodigoPostal, why don't you want to do that?


Top
 Profile  
 
 Post subject: Re: Help mapping table relationships
PostPosted: Thu Jun 12, 2014 4:15 pm 
Newbie

Joined: Wed Jun 11, 2014 9:41 pm
Posts: 3
davided80 wrote:
Quote:
how do i have to do if i want to indicate that the attribute "codigoPostal" is a FK to another table without having to create another class named CodigoPostal.


I don't think you can, but looking at your db it would make sense to have an entity for CodigoPostal, why don't you want to do that?



i don't want to do another entity beacause CodigoPostal has already predefined values and i don't see the point on making another class for something of its nature beacause i won't be creating any new columns in that table neither. If i'm wrong please correct me.

The data that i'll store in the table codigoPostal is the area code, the zip code and some other stuff that refers to cities and countries that i'll store once

here's the codigoPostal table:
Code:
-- -----------------------------------------------------
-- Table `sistemaFacturacion`.`codigoPostal`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `sistemaFacturacion`.`codigoPostal` (
  `codigoPostal` INT NOT NULL,
  `provincia` VARCHAR(25) NOT NULL,
  `localidad` VARCHAR(25) NOT NULL,
  `caracteristicaTel` VARCHAR(10) NOT NULL,
  PRIMARY KEY (`codigoPostal`))
ENGINE = InnoDB;


thanks and sorry for my english :)


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.