Foreign key constraint using @ManyToMany does not work

268 views
Skip to first unread message

roman.ri...@gmail.com

unread,
Aug 13, 2014, 1:35:53 PM8/13/14
to play-fr...@googlegroups.com
Hi,

I'm trying to use @ManyToMany model in ebean (see source code for play framework below). I would like to have a foreign key constraint. So that if a Subject object is linked by Target it should not be possible to remove it until this association exists.
Similar definition for @OneToMany - @ManyToOne is working good.

But for @ManyToMany I'm failing by compilation with the error:

2014-08-13 15:55:02,257 [error] c.a.e.s.d.BeanDescriptorManager - Error in deployment
javax.persistence.PersistenceException: Error with the Join on [models.Taxonomy.targets]. Could not find the local match for [tid]  Perhaps an error in a @JoinColumn

If I do not use JoinTable and JoinColumn it compiles but has no impact by foreign key constraint check.

I would appreciate any help, links to working samples or ideas.

Best regards,

Roman

@Entity
@Table(name = "taxonomy")
public class Taxonomy extends Model {
    
    @Id
    public Long tid;
 
    //bi-directional many-to-many association to Target
    @ManyToMany(mappedBy="subject_to_target", cascade=CascadeType.PERSIST)
    @JoinTable(name = "subject_target", joinColumns = { @JoinColumn(name = "id_target") },
        inverseJoinColumns = { @JoinColumn(name = "id_taxonomy") })
    public List<Target> targets = new ArrayList<Target>();
 

@Entity
@Table(name = "target")
public class Target extends Model {

    @Required
    @Id
    public Long nid;      
  
    //bi-directional many-to-many association to Taxonomy
    @ManyToMany
    @JoinTable(name = "subject_target", joinColumns = { @JoinColumn(name = "id_taxonomy") },
        inverseJoinColumns = { @JoinColumn(name = "id_target") })
    public List<Taxonomy> subject_to_target = new ArrayList<Taxonomy>();

roman.ri...@gmail.com

unread,
Aug 14, 2014, 4:13:24 AM8/14/14
to play-fr...@googlegroups.com
I found configuration with that it compiles and allows to add ojects. But I have a problem, that by removing of the objects (even if I do it on both sides) - entries in the JoinTable are not removed and therefore, I always have foreign key constraint violation. Only if I delete these JoinTable entries manually per ebean SQL Query it seems to work. But is there a better way to remove JoinTable entries? I thought that should happen automatically?

Regards,


Roman

@Entity
@Table(name = "taxonomy")
public class Taxonomy extends Model {
    
    @Id
    @Column(name="ID")

    public Long tid;
 
    //bi-directional many-to-many association to Target
    @ManyToMany
    @JoinTable(name = "subject_target", joinColumns = { @JoinColumn(name = "id_taxonomy", referencedColumnName="ID") },
        inverseJoinColumns = { @JoinColumn(name = "id_target", referencedColumnName="ID") })
    private List<Target> targets = new ArrayList<Target>();
 
    public List<Target> getTargets() {
        return this.targets;
    }
   
    public void setTargets(List<Target> targets) {
        this.targets = targets;

    }  


@Entity
@Table(name = "target")
public class Target extends Model {

    @Required
    @Id
    @Column(name="ID")
    public Long nid;
   
    @ManyToMany(mappedBy="targets")

    public List<Taxonomy> subject_to_target = new ArrayList<Taxonomy>();
   
TargetController.java:
                SqlUpdate removeOldAssociation = Ebean.createSqlUpdate("DELETE FROM subject_target WHERE id_target = " + target.nid);
                removeOldAssociation.execute();

jfak

unread,
Aug 15, 2014, 4:47:03 AM8/15/14
to play-fr...@googlegroups.com
Hi,
you can see in the documentation Java API, class Model
the methods: saveManyToManyAssociations and  deleteManyToManyAssociations do the work.
regards,
François




Reply all
Reply to author
Forward
0 new messages