Extending domain class with one-to-many relationship in Grails - grails

Would this be correct way to extend Grails parent and child classes?
Originally I thought that overriding hasMany and belongsTo properties, would be a good idea but that did not work so well as it introduced conflicting functionality so I dropped it from subclasses.
What I am trying to do here is to package shared code between multiple applications. I am starting with these two classes in my plugin.
class Purchase {
String firstName
String lastName
static mapping = {
tablePerHierarchy false
}
static hasMany = [items: PurchaseItem]
}
class PurchaseItem {
BigDecimal price
Integer qty
statiuc belongsTo = [purchase: Purchase]
static mapping = {
tablePerHierarchy false
}
}
The application specific classes have to extend both Purchase and PurchaseItem so I am implementing it like so, inheriting one-to-many relationship:
class Flight {
static hasMany = [purchases: TicketPurchase]
}
class TicketPurchase extends Purchase {
// some class specific properties
static belongsTo = [flight: Flight]
}
class TicketPurchaseItem extends PurchaseItem
Integer bagQty
static namedQueries = {
ticketPurchaseItemsWithBagsByFlight {flightInstance->
purchase {
flight {
eq 'id', flightInstance.id
}
}
gt 'bagQty', 0
}
}
}
The namedQuery in TicketPurchaseItem joins Purchase and Flight even though super class Purchase does not belongTo Flight, only subclass TicketPurchase does.
TicketPurchase ticketPurchase = new TicketPurchase()
ticketPurchase.addToItems(new TicketPurchaseItem(bagQty: 5)).save()
Flight flight = Flight.first()
flight.addToPurchases(ticketPurchase).save()
// this works
def ticketPurchaseItemList = TicketPurchaseItem.ticketPurchaseItemsWithBagsByFlight(flight)
This works with Grails but is it good design or is there a better way to deal with domain classes extending one-to-many relationships?

The short answer is you've got it right. Probably. The question to ask is whether you're ok with allowing the properties you've added to your subclasses to be set to NULL. I don't see a problem with what you have. You can learn more about Grails domain class inheritance and polymorphic queries from the Grails documentation and from my blog article on the subject.
If you're curious about the impact of your domain class model on the database, you can take a look at the queries GORM/Hibernate is running by logging then. I believe this is the article I've used to set up logging.

Related

Grails 3 hasOne nullability issue

While migrating an existing app from Grails 2.5 to 3.1, I ran into an odd issue with a bi-directional one-to-one relationship.
Imagine a simple model with a User and Employee objects. A User represents a generic user account. Not all users are Employees but all Employees are Users. Moreover Employees have references to managers, supervisors, etc (also User instances). User is the owning side of the relationship.
class User {
Employee employee
static mappedBy = [employee: "user"]
static hasOne = [employee: Employee]
static constraints = {
employee(nullable:true)
}
}
class Employee {
User user // represents employee's own account, a bi-directional one-to-one
User supervisor // represents a supervisor
static belongsTo = [user: User]
static constraints = {
user(unique:true)
supervisor(nullable:true)
}
}
The trouble after upgrading to Grails 3 is that in the create mode, this results in supervisor_id column of employee table being generated as NOT NULL, whereas in Grails 2 it was nullable as expected (with only user_id being NOT NULL).
I tested this with Grails 3.1.12 and 3.2.0, getting the same behavior with both. Am I doing anything stupid in my domain class declarations? I've tried multiple mappings to achieve the same behavior as in Grails 2.5 without luck. In some cases I'm even getting a foreign key on both sides of the relationship...
I don't know why your code was working with previous version of Grails, but it is wrong.
When you use hasMany and belongsTo, it is not necessary to define other property in the child object, and you don't need also to use the mappedBy property on the parent, and the same with the parent (property employee at User)
Grails doesn't need anything else to know which is the bidirectional property on both classes, and the constraint user(unique : true) neither.
So your classes should look like this:
class User {
static hasOne = [employee: Employee]
static constraints = {
employee(nullable: true)
}
}
class Employee {
User supervisor // represents a supervisor
static belongsTo = [user: User]
static constraints = {
supervisor(nullable:true)
}
}
It could be nice to know how is your DB structure. But in this way all foreign keys are stored in the employee table. But of course you could navigate from both entities. If you have different structure you could map your current database with this model. See this
employee.user
user.employee

Why do we need explicit relationships in grails?

I am a beginner in GRAILS so i am hoping some help on the issue i am facing.
I have read the documentation but i am still vague on the idea of relationships in grails. In grails, you could have 4 types of relationship between domain classes.
1 to 1
1 to many
many to 1
many to many
Grails has three constructs to define relationships
static hasMany =
static belongsTo =
static hasOne =
My question and dilemma is why do we need these three constructs to define a relation when we could just specify what type of objects each class has that would automatically define relationship between domain classes.
for example
To define many to many i could have two classes designed this way
class Author{
Set<Book> books
}
class Book{
Set<Author> authors
}
For 1 to many and many to 1
class Author{
Set<Book> books
}
class Book{
String title
}
for one to one
class Author{
Book book
}
class Book{
Author author
}
I appreciate it if anyone can give me a clear, easy to understand explanation. Thank you!
Everything you defined there should work fine. You don't have to use any of the other stuff that you mentioned that GORM offers, but there are reasons that you might want to. For example, you can write a class like this:
class Author{
Set<Book> books
}
That is not the same thing as this:
class Author {
static hasMany = [books: Book]
}
When you use hasMany, Grails generates this for you...
class Author {
Set<Book> books
def addToBooks(Book b) {
books.add(b)
this
}
def addToBooks(Map m) {
books.add(new Book(m))
this
}
def removeFromBooks(Book b) {
books.remove(b)
this
}
}
That isn't exactly what is generated, but that is some of the stuff that you might care about.
There is more to it than is represented there. For example, if the Book has a reference back to the Author, the addToBooks methods will hook that back reference up for you.
There are other behaviors associated with the other properties you mentioned. For example, the hasOne property switches the direction in which the foreign key points on the persistence model. The belongsTo property enforces cascading of certain events. etc.
Take a look at the GORM docs at http://grails.org/doc/latest/guide/GORM.html for more information.

Creating one-to-many & many-to-many for same domain class in grails

I want to create a domain class as like , One user can post many orders [Bidirectional] and one order can be liked by many users [unidirectional].
I have written a domain class as shown below ,
Class User {
String userName;
List orders
static hasMany = [Order]
}
Class Order {
String orderId
String orderName
//Indicates this order belongs to only one user
static belongsTo =[owner : User ] // Bidirectional
//Indicates order can be liked by many users
static hasMany = [likedUser : User] //Unidirectional
}
But I am getting am error saying invalid schema . Any body please help...
This post looks similar to my question but I am not getting , Please help.
First, order is a reserved word in SQL. Since GORM by default creates a table with the same name as your class, you'll need to either rename your class or provide a different name to use when mapping to SQL tables.
For example:
class Order {
static mapping = {
table 'user_order'
}
// ...
}
Another problem is that Order contains two associations to User. You need to tell GORM which one of these that is the bi-directional association from User to Order. That can be achieved using mappedBy, like this:
class User {
String userName
static hasMany = [orders: Order]
static mappedBy = [orders: 'owner']
}
Hope this helps.

How does one mix 'Reference' and 'No Reference' belongTo relationships in one Domain Class?

In Grails belongsTo allows one domain class to establish a cascading relationship with another domain class. There are two styles of relationships when using belongsTo: Reference and No Reference. Reference creates a property on the owned object while No Reference merely establishes an invisible GORM relationship.
Example parent domain-class:
class Car {
Engine engine
}
belongsTo without Reference property:
class Engine {
static belongsTo = Car
}
belongsTo with Reference property:
class Engine {
static belongsTo = [car:Car]
}
Not to hard right, however the trouble for me starts when we start using multiple belongsTo references:
belongsTo with multiple back references:
class Engine {
static belongsTo = [car:Car, user:User]
}
multiple belongsTo relationships without property references:
class Engine {
static belongsTo = [Car, User]
}
Here's the problem, how do I mix the two above styles?
Say I want a property reference for the User but not for the Car, how would I write that belongsTo call?
Any information on how to mix No Reference relationship links with Reference property in a single domain class would help.
Links:
Using Grails Object Relational Mapping (GORM)
belongsTo - grails.org
This question reposted by me on the official Grails forum
class Engine {
User user
static belongsTo = [Car, User]
}
That said, I always use the map (reference) syntax over the list (no reference) syntax because I like mine to be bi-directional.

Grails many-to-many relationship across the same table

I'm using Grails and I want to have a unidirectional many-to-many relationship.
In my application, an Employee can "bookmark" another Employee in order to quickly access them to leave notes. Employees need not know who has bookmarked them.
In other words, I essentially want to have an employee.bookmarks property that I can use for this.
Thus far, I've found the documentation on many-to-many relationships for Grails ORM, but this seems to be exclusively across two different tables.
Sounds like you just need a regular unidirectional 1-many:
class Employee {
...
static hasMany = [bookmarks: Employee]
}
You can use transient properties and an additional table "Bookmark" to do this task:
class Employee {
String name
static transients = ["bookmarks"]
def getBookmarks() {
Bookmark.findAllByBookmarker(this, [sort: "id", order: "asc"])
}
...
}
class Bookmark implements Serializable {
Employee bookmarker // the employee who bookmark someone
Employee bookmarkee // the employee who is bookmarked
static mapping = {
table "Bookmark"
id composite: ['bookmarker', 'bookmarkee']
bookmarker(column: "BOOKMARKER_ID")
bookmarkee(column: "BOOKMARKEE_ID")
version false
}
static Bookmarker get(long bookmarkerId, long bookmarkeeId) {
find 'from Bookmark where bookmarker.id=:bookmarkerId and bookmarkee.id=:bookmarkeeId',
[bookmarkerId: bookmarkerId, bookmarkeeId: bookmarkeeId]
}
...
}
This method uses table "Bookmark" to store the relations between employees, so it is possible to have 2 people bookmark the same employee. Note that class Bookmark must implements Serializable.

Resources