Query with a field in composite key

B

Biagio

Hi,

when I do a query using a field in a composite key I get an error

org.hibernate.exception.SQLGrammarException: could not execute query

@OneToOne
@JoinColumn(name = "condizioniPK.codlinea",
referencedColumnName="codlinea", unique = true, nullable = false,
insertable = false, updatable = false)
private Linee linee;

I do not understand what is wrong. The same query without the relation
works fine.

Any suggestions? Thanks

Biagio
 
J

Jean-Baptiste Nizet

Hi,

when I do a query using a field in a composite key I get an error

org.hibernate.exception.SQLGrammarException: could not execute query

@OneToOne
    @JoinColumn(name = "condizioniPK.codlinea",
referencedColumnName="codlinea", unique = true, nullable = false,
insertable = false, updatable = false)
    private Linee linee;

The name attribute of the OneToOne annotation is suppsed to contain
the name of the column of the database table. I doubt the column name
is "condizioniPK.codlinea".
 
B

Biagio

The name attribute of the OneToOne annotation is suppsed to contain
the name of the column of the database table. I doubt the column name
is "condizioniPK.codlinea".

But if I write:

@OneToOne
@JoinColumn(name = "codlinea", referencedColumnName="codlinea",
unique = true, nullable = false, insertable = false, updatable =
false)
private Linee linee;


the error is on EntityManagerFactory

Exception in thread "main" javax.persistence.PersistenceException:
[PersistenceUnit: HTraspPU] Unable to build EntityManagerFactory

Thanks

Biagio
 
L

Lew

But if I write:

@OneToOne
@JoinColumn(name = "codlinea", referencedColumnName="codlinea",
unique = true, nullable = false, insertable = false, updatable =
false)
private Linee linee;


the error is on EntityManagerFactory

Exception in thread "main" javax.persistence.PersistenceException:
[PersistenceUnit: HTraspPU] Unable to build EntityManagerFactory

An http:/sscce.org/ would help.

Is the join column name 'codlinea' in both tables?

What is the DDL for both tables?

What is this mysterious unrevealed query that is such a big secret?
 
B

Biagio

Biagio said:
But if I write:
@OneToOne
      @JoinColumn(name = "codlinea", referencedColumnName="codlinea",
unique = true, nullable = false,  insertable = false, updatable =
false)
      private Linee linee;
the error is on EntityManagerFactory
Exception in thread "main" javax.persistence.PersistenceException:
[PersistenceUnit: HTraspPU] Unable to build EntityManagerFactory

An http:/sscce.org/ would help.

Is the join column name 'codlinea' in both tables?

What is the DDL for both tables?

What is this mysterious unrevealed query that is such a big secret?

I use Netbeans 6.9 and Hibernate.
This is my situation:

Table: Condizioni Table: Linee
+--------+---------+--------+ +--------+----------+
|codcli |codlinea |prezzo | |codlinea|deslinea |
+--------+---------+--------+ +--------+----------+
K K K

| |
+-----------------------+
@Entity
@Table(name = "condizioni")
@NamedQuery(name = "Condizioni.findAll", query = "SELECT c FROM
Condizioni c")
public class Condizioni implements Serializable {

private static final long serialVersionUID = 1L;
@EmbeddedId
protected CondizioniPK condizioniPK;
@Basic(optional = false)
@Column(name = "prezzo", nullable = false)
private float prezzo;
@OneToOne
@JoinColumn( name="codlinea", unique = true, nullable = false,
insertable = false, updatable = false)
private Linee linee;

@Embeddable
public class CondizioniPK implements Serializable {
@Basic(optional = false)
@Column(name = "codcli", nullable = false)
private int codcli;
@Basic(optional = false)
@Column(name = "codlinea", nullable = false)
private int codlinea;


@Entity
@Table(name = "linee")
public class Linee implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@Column(name = "codlinea", nullable = false)
private Integer codlinea;
@Basic(optional = false)
@Column(name = "deslinea", nullable = false, length = 30)
private String deslinea;

This code produce this error:

Exception in thread "main" javax.persistence.PersistenceException:
[PersistenceUnit: HTraspPU] Unable to build EntityManagerFactory


If I change in

@OneToOne
@JoinColumn( name="condizioniPK.codlinea", unique = true,
nullable = false, insertable = false, updatable = false)
private Linee linee;

CondizioniJpaController em = new CondizioniJpaController();
java.util.List tabellaList =
em.getEntityManager().createNamedQuery("Condizioni.findAll").getResultList();

the error is

Exception in thread "AWT-EventQueue-0"
javax.persistence.PersistenceException:
org.hibernate.exception.SQLGrammarException: could not execute query

Without @OneToOne annotation all works fine.

Help

Biagio
 
L

Lew

Biagio said:
I use Netbeans 6.9 and Hibernate.
This is my situation:

Table: Condizioni                 Table: Linee
+--------+---------+--------+     +--------+----------+
|codcli  |codlinea |prezzo  |     |codlinea|deslinea  |
+--------+---------+--------+     +--------+----------+
    K        K                       K

             |                       |
             +-----------------------+
@Entity
@Table(name = "condizioni")
@NamedQuery(name = "Condizioni.findAll", query = "SELECT c FROM
Condizioni c")
public class Condizioni implements Serializable {

    private static final long serialVersionUID = 1L;
    @EmbeddedId
    protected CondizioniPK condizioniPK;
    @Basic(optional = false)
    @Column(name = "prezzo", nullable = false)
    private float prezzo;
@OneToOne
    @JoinColumn( name="codlinea",  unique = true, nullable = false,
insertable = false, updatable = false)
    private Linee linee;

@Embeddable
public class CondizioniPK implements Serializable {
    @Basic(optional = false)
    @Column(name = "codcli", nullable = false)
    private int codcli;
    @Basic(optional = false)
    @Column(name = "codlinea", nullable = false)
    private int codlinea;

@Entity
@Table(name = "linee")
public class Linee implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @Column(name = "codlinea", nullable = false)
    private Integer codlinea;
    @Basic(optional = false)
    @Column(name = "deslinea", nullable = false, length = 30)
    private String deslinea;

This code produce this error:

Exception in thread "main" javax.persistence.PersistenceException:
[PersistenceUnit: HTraspPU] Unable to build EntityManagerFactory

If I change in

@OneToOne
    @JoinColumn( name="condizioniPK.codlinea",  unique = true,
nullable = false, insertable = false, updatable = false)
    private Linee linee;

CondizioniJpaController em = new CondizioniJpaController();
java.util.List tabellaList =
em.getEntityManager().createNamedQuery("Condizioni.findAll").getResultList(­);

the error is

Exception in thread "AWT-EventQueue-0"
javax.persistence.PersistenceException:
org.hibernate.exception.SQLGrammarException: could not execute query

Without @OneToOne annotation all works fine.

The table relationship is not one to one, so your annotation lies to
the system.
 
B

Biagio

Biagio said:
I use Netbeans 6.9 and Hibernate.
This is my situation:
Table: Condizioni                 Table: Linee
+--------+---------+--------+     +--------+----------+
|codcli  |codlinea |prezzo  |     |codlinea|deslinea  |
+--------+---------+--------+     +--------+----------+
    K        K                       K
             |                       |
             +-----------------------+
@Entity
@Table(name = "condizioni")
@NamedQuery(name = "Condizioni.findAll", query = "SELECT c FROM
Condizioni c")
public class Condizioni implements Serializable {
    private static final long serialVersionUID = 1L;
    @EmbeddedId
    protected CondizioniPK condizioniPK;
    @Basic(optional = false)
    @Column(name = "prezzo", nullable = false)
    private float prezzo;
@OneToOne
    @JoinColumn( name="codlinea",  unique = true, nullable = false,
insertable = false, updatable = false)
    private Linee linee;
@Embeddable
public class CondizioniPK implements Serializable {
    @Basic(optional = false)
    @Column(name = "codcli", nullable = false)
    private int codcli;
    @Basic(optional = false)
    @Column(name = "codlinea", nullable = false)
    private int codlinea;
@Entity
@Table(name = "linee")
public class Linee implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @Column(name = "codlinea", nullable = false)
    private Integer codlinea;
    @Basic(optional = false)
    @Column(name = "deslinea", nullable = false, length = 30)
    private String deslinea;
This code produce this error:
Exception in thread "main" javax.persistence.PersistenceException:
[PersistenceUnit: HTraspPU] Unable to build EntityManagerFactory
If I change in
@OneToOne
    @JoinColumn( name="condizioniPK.codlinea",  unique = true,
nullable = false, insertable = false, updatable = false)
    private Linee linee;
CondizioniJpaController em = new CondizioniJpaController();
java.util.List tabellaList =
em.getEntityManager().createNamedQuery("Condizioni.findAll").getResultList(­);
the error is
Exception in thread "AWT-EventQueue-0"
javax.persistence.PersistenceException:
org.hibernate.exception.SQLGrammarException: could not execute query
Without @OneToOne annotation all works fine.

The table relationship is not one to one, so your annotation lies to
the system.

Yessssssssssssssssss

with @ManyToOne works !!!!!!

Many Thanks

Biagio
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,981
Messages
2,570,188
Members
46,731
Latest member
MarcyGipso

Latest Threads

Top