Mediouni hatem
Mediouni hatem

Reputation: 61

JPA with @ManyToOne

I have two classes like this

package models;
import java.util.*;
import javax.persistence.*;
import play.db.jpa.*;

@Entity
@Table(name = "commitment_type_value")
public class CommittmentTypeValue extends Model{    
    @Id
    @Column(name = "id", nullable = false)
    public Long id;    
    @Column(name = "value", nullable = true)
    public String type;    
    @ManyToOne
    @JoinColumn(name="commitment_type_id")
    public CommitementType commitmentType;    
    public CommittmentTypeValue(){

    }
}

-------------


package models;

import java.util.*;

import javax.persistence.*;

import play.db.jpa.*;


/**
 *
 * @author hatem
 */
@Entity
@Table(name = "commitment_type")
public class CommitementType extends Model{

    @Id
    @Column(name = "id", nullable = false)
    public Long id;

    @Column(name = "type", nullable = true)
    public String type;

    @OneToMany(fetch=FetchType.LAZY, cascade = CascadeType.ALL, mappedBy="commitmentType")
    public List<CommittmentTypeValue> commitmentTypeValues;

    public CommitementType(){

    }
}

when I execute my app, this problem appears :

A JPA error occurred (Unable to build EntityManagerFactory): A Foreign key refering models.CommitementType from models.CommittmentTypeValue has the wrong number of column. should be 2

Please, can Any one tell me what 's wrong ?

Upvotes: 1

Views: 1632

Answers (3)

Badal Singh
Badal Singh

Reputation: 918

Please check your foreign key column name it should match the exactly with the name of column.


EDIT If your problem is still unsolved then please check if your persistance.xml has

<property name="generateDdl" value="true" />

and if it already have then check if you are getting any error in generation of table.

If yes then clear the data in table or add drop-and-create-tables option in config file or change your code as follows

@ManyToOne(optional=true)
@JoinColumn(name="commitment_type_id", nullable = true)
public CommitementType commitmentType;

Because you might have old data in table which might be stopping the creation of new table.

Upvotes: 3

James
James

Reputation: 18389

The error sounds like you Id in CommitementType is composite, so your foreign key must contain two columns.

Include the code for the CommitementType.

Upvotes: 0

vels4j
vels4j

Reputation: 11298

Reference column name is missing in your many to one join in class CommittmentTypeValue

@ManyToOne
@JoinColumn(name="commitment_type_id" referencedColumnName="id" )
public CommitementType commitmentType;  

Also specify target entity

@OneToMany(fetch=FetchType.LAZY, cascade = CascadeType.ALL, targetEntity=CommittmentTypeValue.class, mappedBy="commitmentType")
public List<CommittmentTypeValue> commitmentTypeValues;

Upvotes: 0

Related Questions