Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unidirectional OneToMany relationship without joinTable

In order to achieve a foreign key column (no joinTable) when mapping a unidirectional OneToMany relationship, i follow the steps according to Grails (version 2.2.3) reference documentation, section 6.5.2.1 OneToMany mapping, which states

With unidirectional associations the foreign key needs to be specified on the association itself. For example given a unidirectional OneToMany relationship between Person (replaced by One) and Address (replaced by Many), the following code will change the foreign key in the Many table

class One {

    static hasMany = [manyCollection:Many]
    static mapping = {
        manyCollection(column:"ONE_ID")
    }

}

However, when i run

One one = new One()
one.addToManyCollection(new Many())

one.save()

I get in the console

insert 
into
    one
    (id, version) 
values
    (null, ?)

insert 
into
    many
    (id, version) 
values
    (null, ?)

insert 
into
    one_many
    (one_many_collection_id, many_id) 
values
    (?, ?)

Notice Grails create a joinTable called one_many. So, my question: it is a bug or something else ? What should i do to get rid of the joinTable ?

Even when i use something like

class One {

    static hasMany = [manyCollection:Many]
    static mapping = {
        manyCollection(joinTable:false)
    }

}

as highlighted here, i get the same output

like image 841
Arthur Ronald Avatar asked Aug 05 '13 05:08

Arthur Ronald


People also ask

Is OneToMany unidirectional?

– @OneToMany annotation is for One-to-Many Unidirectional mapping. We use it together with @JoinColumn annotation that will create a column in child table storing the parent entity id ( tutorial_id ).

What is the default mechanism for representing one-to-many unidirectional relationships in JPA?

As straightforward as it might be in a relational database, when it comes to JPA, the one-to-many database association can be represented either through a @ManyToOne or a @OneToMany association since the OOP association can be either unidirectional or bidirectional.

What makes a one-to-many relationship as bidirectional relationship?

The way this works at the database level is we have a cart_id as a primary key in the cart table and also a cart_id as a foreign key in items. The way we do it in code is with @OneToMany. We can also add a reference to Cart in each Item using @ManyToOne, making this a bidirectional relationship.

Can one-to-many be unidirectional?

Here you can see an example of a PurchaseOrder entity that models a unidirectional one-to-many relationship to the Item entity.


1 Answers

You're almost there, you have 2 halves of the mapping but you need both for it to work correctly. It's not a well documented feature but I've used this before and it definitely works (e.g. no join table). So in your One class,

class One {
  ..
  static mapping = {
    manyCollection column: "ONE_ID", joinTable: false
  }
}

NOTE: it's probably a good idea to exit, clean and restart grails when making mapping changes. I've lost many hours banging my head when a simple grails clean was the solution.

Also, you could technically add belongsTo without the back reference in your Many class and you would still have a true uni-directional but with the added benefit of cascading action (mapped in One class) if it's something you need.

like image 193
ikumen Avatar answered Sep 17 '22 09:09

ikumen