users@glassfish.java.net

Re: _at_ManyToOne when Foreign Key is also Primary Key

From: Gregory Gerard <ggerard_at_mac.com>
Date: Wed, 20 Oct 2010 14:48:58 -0700

I've done this successfully as a separate PK class with the int ids and the @Id atop the ManyToOne.

public class ClientCardPK {
private long mClient;
private long mCard;
}

@Entity
@IdClass(ClientCardPK.class)
@Table(name = "ClientCard")
public class ClientCard {
    @Id
    @ManyToOne
    @JoinColumn(name = "Client_Id", referencedColumnName = "Client_Id")
    private Client mClient;

    @Id
    @ManyToOne
    @JoinColumn(name = "Card_Id", referencedColumnName = "Card_Id")
    private Card mCard;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "ClientCard_AssociationDate", nullable = false)
    private final Date mAssociationDate = new Date();
}

On Oct 20, 2010, at 08:33, glassfish_at_javadesktop.org wrote:

> Hello!
> I don't know whether I am posting my thread in the right place or no. If I am not, let me know.
>
> I am starting with JPA but I found a problem with @ManyToOne relation when the foreign key is part of the primary key.
>
> Here is my database:
>
> [b]Table Client[/b]
> cli_id_client (PK)
>
> [b]Table Card[/b]
> cli_id_card (PK)
>
> [b]Table ClientCard[/b]
> cli_id_client (PK)
> cli_id_card (PK)
>
>
> Those are my entities:
>
>
> public class [b]ClientCard[/b]{
> @Id
> @Column(name = "car_id_card")
> private int idCard;
>
> @Id
> @Column(name = "cli_id_client")
> private int idClient;
>
> @ManyToOne(fetch = FetchType.LAZY)
> @PrimaryKeyJoinColumns({_at_PrimaryKeyJoinColumn(name = "car_id_card", referencedColumnName = "car_id_card")})
> private Card card;
>
> @ManyToOne(fetch = FetchType.LAZY)
> @PrimaryKeyJoinColumns({_at_PrimaryKeyJoinColumn(name = "cli_id_client", referencedColumnName = "cli_id_client")})
> private Client client;
>
> }
>
> public class [b]Client[/b]{
> ..
> @Id
> @Column(name = "cli_id_client")
> private int idClient;
>
> @OneToMany(mappedBy="Client")
> private java.util.List<ClientCard> listClientCard;
> }
>
> public class [b]Card[/b]{
> ..
> @Id
> @Column(name = "car_id_card")
> private int idCard;
>
> @OneToMany(mappedBy = "card")
> private java.util.List<ClientCard> listClientCard;
> }
>
>
> Unfortunately, when I try persist the ClientCard, I receive the message:
>
> 08:21:22,711 WARN [JDBCExceptionReporter] SQL Error: 207, SQLState: S0022
> 08:21:22,711 ERROR [JDBCExceptionReporter] [HOST]Invalid column name 'card_car_id_card'.
> 08:21:22,711 WARN [JDBCExceptionReporter] SQL Error: 207, SQLState: S0022
> 08:21:22,711 ERROR [JDBCExceptionReporter] [HOST]Invalid column name 'client_cli_id_client'.
>
>
> Am I missing any configuration?
> [Message sent by forum member 'danilorocha']
>
> http://forums.java.net/jive/thread.jspa?messageID=485747
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_glassfish.dev.java.net
> For additional commands, e-mail: users-help_at_glassfish.dev.java.net
>