Id jednej klasy ma być id klasy rozszerzającej

0

Mam klasę Address i klasę Area. Area ma wszystko od Address plus dodatkowo szerokość i długość geograficzną. Wszystkie kolumny Address muszą być w bazie unikatowe, chcę więc nie robić dodatkowego id dla Area tylko użyć foreign key jakim jest ADDRESS_ID. Niestety JPA się buntuje:
org.hibernate.MappingException: Foreign key (FKrjn6s3ktaanm7d01hk0ii8jfu:route [])) must have same number of columns as the referenced primary key (area [address_id])
tak o chcę:
title

kod:

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import javax.validation.constraints.Pattern;

import org.hibernate.annotations.Immutable;
import org.springframework.validation.annotation.Validated;

@Validated
@Entity
@Immutable
@Table(uniqueConstraints = @UniqueConstraint(columnNames = { "city", "street", "houseNumberEtc", "postalcode" }))
public class Address {

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

    @Column(updatable = false)
    public final String city;

    @Column(updatable = false)
    public final String street;

    @Column(updatable = false)
    public final String houseNumberEtc;

    @Pattern(regexp = "^\\d{2}-\\d{3}$")
    @Column(updatable = false)
    public final String postalCode;

    @OneToOne(mappedBy="address")
    private Area area;

    protected Address() {
        this.city = null;
        this.street = null;
        this.houseNumberEtc = null;
        this.postalCode = null;
    }

    public Address(String city, String street, String houseNumberEtc, String postalCode) {
        this.city = city;
        this.street = street;
        this.houseNumberEtc = houseNumberEtc;
        this.postalCode = postalCode;
    }

    public Long getId() {
        return id;
    }

    @Override
    public String toString() {
        return "Address [" + city + street + houseNumberEtc + postalCode + "]";
    }
}

.

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.MapsId;
import javax.persistence.OneToOne;
import javax.validation.constraints.DecimalMax;
import javax.validation.constraints.DecimalMin;

import org.hibernate.annotations.Immutable;


@Entity
@Immutable
public class Area {

    @Id
    private Long id;

    @OneToOne
    @JoinColumn(name = "address_id")
    @MapsId
    public final Address address;

    @DecimalMax(value = "180.0")
    @DecimalMin(value = "-180.0")
    @Column(updatable = false)
    public final double longitude;

    @DecimalMax(value = "90.0")
    @DecimalMin(value = "-90.0")
    @Column(updatable = false)
    public final double latitude;


    protected Area() {
        this.address = null;
        this.longitude = 0;
        this.latitude = 0;
    }

    public Area(Address address, double longitude, double latitude) {
        this.address = address;
        this.longitude = longitude;
        this.latitude = latitude;
    }

    public Long getId() {
        return id;
    }

    @Override
    public String toString() {
        return address.toString();
    }
}

próbowałem dodać MapsId, ale niestety:

org.hibernate.PersistentObjectException: detached entity passed to persist: Address

0
Pinek napisał(a):

A spróbuj @PrimaryKeyJoinColumn, zgodnie z tym:
http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html_single/#entity-mapping-association

no to chyba nie pozostaje mi nic innego jak ręcznie zapodawać tego idka:

@Entity
@Immutable
public class Area {

	@Id
	@Column(name = "address_id")
	private Long id;
	
	@OneToOne
	@PrimaryKeyJoinColumn
	public final Address address;

	@DecimalMax(value = "180.0")
	@DecimalMin(value = "-180.0")
	@Column(updatable = false)
	public final double longitude;

	@DecimalMax(value = "90.0")
	@DecimalMin(value = "-90.0")
	@Column(updatable = false)
	public final double latitude;

	protected Area() {
		this.id = 0L;
		this.address = null;
		this.longitude = 0;
		this.latitude = 0;
	}

	public Area(Address address, double longitude, double latitude) {
		this.id = address.getId();
		this.address = address;
		this.longitude = longitude;
		this.latitude = latitude;
	}

	public Long getId() {
		return id;
	}
	
	public void setId(long id) {
		this.id = id;
	}
	
	@Override
	public String toString() {
		return address.toString();
	}
}

1 użytkowników online, w tym zalogowanych: 0, gości: 1