I am having to two domain classes Order and Stock. When stock is sold I am creating an entry in the child table StockOrder which contains information about the Order(order_id) and Stock(stock_id) and noOfStockSold.
In my current design I coded the StockOrder close to Stock table. You can see this below.
Class Stock {
String stockName
BigDecimal quantity
List stockOrderList
static hasMany = [stockOrderList: StockOrder]
}
class StockOrder {
Stock stock
Order order
BigDecimal noOfStockSold
static belongsTo = [Stock]
}
class Order {
List saleLineItemList
static hasMany = [saleLineItemList: SaleLineitem]
}
Am I doing to correctly from ERP prespective. How to relate Order to Stock sold?
Is it ok if I tie StockOrder to Order also by doing static belongsTo = [Stock,Order]
Is there any better way of doing it or any improvements?
I would start by reading these:
http://grails.org/doc/2.0.x/ref/Domain%20Classes/belongsTo.html
http://grails.org/doc/2.0.x/ref/Domain%20Classes/hasMany.html
Basically you use the belongsTo and hasMany to describe bi-directional relationships. This allows you to cascade delete objects if you so desire. I would imagine in an ERP system that you wouldn't want cascading functionality because if you delete a Stock you probably don't want to delete all the associated StockOrder. I would probably keep the hasMany side of the relationship and remove the belongsTo since you're already associating a StockOrder to a Stock and an Order.
Related
What is the different in GORM between
class Books {
Author author
}
and
class Books {
static belongsTo = [author: Author]
}
Does cascading rules changes in these two approaches? Also, when to use belongsTo and more importantly, when not to use belongsTo in Grails ?
Yes, belongsTo is intended for controlling cascades of saves and deletes. You may reference the full documentation here http://docs.grails.org/latest/ref/Domain%20Classes/belongsTo.html but to summarize (and in case the URL dies someday):
Use belongsTo to indicate ownership. Saves or deletes to the parent will cascade to the child. In your example, if the Author is deleted, his Books will be too (assuming Author hasMany Books
Do not use belongsTo if you just want to indicate a relationship with no ownership on either side and no automatic cascading of saves or deletes.
class Cart {
static hasMany = [list: Fruit]
}
Say we made a domain in Grails with a hasMany relationship with another model. So in our service, if we want to update the list for an object, is it better to replace the entire list with a new list, or is it better to individually add/remove the items from the list?
I am learning grails and I came up with a use case. In my use case, a product has many users and each user can have many roles.
Here is my product class:
class Product {
String name
String description
String vision
Date startDate
Date endDate
static hasMany = [users : User, contributors : User, watchers : User, approvers : User]
static belongsTo = User
static constraints = {
}
}
Here is my User class:
class User {
static constraints = {
}
String fullName
String email
static hasMany = [roles : Roles, products : Product]
}
Here is the Roles Enum:
public enum Roles {
PRODUCTOWNER ('ProductOwner'),
APPROVER ('Approver'),
CONTRIBUTOR ('Contributor'),
WATCHER ('Watcher')
}
My question is specifically about the association between Product and User. I want to represent the fact that a product can have many users in different roles. Also, each user can be part of multiple products with a different role in each product. Is this the right way to represent this relationship? Also, I should be able to remove and add users to products and vice versa. What this also means is that, users can keep moving between roles and can move in and out of products. In this scenario, I probably don't want cascades to happen. How do I prevent automatic cascades from happening to CRUD operations for this relationship?
Thanks.
I think rather than having roles and products in User.groovy, it will be better if you create a separate domain like UserProductRole. As you said user will have different role in different products then creating a separate domain makes more sense in business usecase and also doing queries
class UserProductRole{
Role role
static belongsTo = [user:User,product:Product]
static constraints = {
user (unique:['product','role']
}
}
You can create composite key but I generally dont perfer it because it makes querying bit difficult.
And now you need to change hasMany in User and Product like following
[userProducts:UserProductRole] rather then having users or products
Hi this have been going on for several months now and I have lost all hope.
I have 3 domains in question.
Product which is made up of Components, each Component can have alternative products.
Here are my models:
Product:
class Product {
String name
String comments
static hasMany = [components:Components]
}
Component:
class Components {
Product product
static hasMany = [alternatives:Alternatives]
}
Alternatives:
class Alternatives {
Product product
}
To populate this I am parsing an excel Spreadsheet here is the code that does it:
Product st = new Product(
name: row.getCell(0).getStringCellValue().toUpperCase(),
comments: "test"
)
row.getCell(10).getStringCellValue().split("[|]").each {
Product pro = Product.findByName(it.toUpperCase())
if(pro) {
Components c = new Components(product: pro).save(flush:true)
s.add(c)
// Does not work
//st.addToComponents(c)
}
}
st.save(flush:true,failOnError:true)
This does not create a table in the database called product_components. this is what I would expect.
When I try and use addTo the component has a product instance associated with it, the product instance is then changed to the st product instance which I am trying to save it to.
I hope that makes sense.
I have found out that it is mapping the product in the Components domain as the component in the Product domain.
I am now assuming that using mapped by in the Product table may fix it
static MappedBy = [components : //do something here ]
Found the answer see bellow:
I had to add the following in my Product domain.
static mappedBy = {components joinTable: [name:'product_components',
column:'COMPONENTS_ID',
key: 'PRODUCT_ID']}
and change the name of product to part_of in my Components domain.
The way you have it set up now, your Components class only allows one product. So you have a one-to-many relationship from Product to Components, this is the reason the join table product_components is not being created.
You are also trying to assign a Components instance to multiple products:
// To the 'pro' Product
new Components(product: pro)
// To the 'st' Product
st.addToComponents(c)
But your Components domain class only allows for one Product, which is why the referenced Product is being overwritten.
If you want your Components class to have more than one product (which is what the code you've written is trying to accomplish), you'll have to add the Product class to your static hasMany block on Components:
class Components {
static hasMany = [alternatives: Alternatives, products: Product]
}
Then you can perform the following:
def c = new Components().save(flush: true)
pro.addToComponents(c)
st.addToComponents(c)
On an unrelated note, I would recommend making your domain class names singular, i.e. Alternative and Component to follow Grails' conventions and to make more sense.
I'm having an issue dealing with two entities which should have a relationship of one to many and many to many. Let me show you the case: there are users, which create and take part of activities. So an activity could have several users and just one activity creator, meanwhile an user can create and belong to many activities.
So I did something like this:
class User {
static hasMany = [activities:Activity, activitiesCreated: Activity]
static mappedBy = [activitiesCreated: "creator"]
...
}
class Activity{
static hasMany = [users:User]
static belongsTo = [users:User]
Usuario creator
...
}
This raises a runtime exception, which is this one:
No owner defined between domain classes [class User] and [class Activity] in a many-to-many relationship. Example: static belongsTo = Activity
The many-to-many relationship works fine if I don't try to implement the one-to-many, so it wouldn't be the problem.
And this is where I'm stuck :/
I would have third entity to realize the many to many relationship. For example, let say represent actual execution of the Activities as an Event, which means a Event has one ore more Activities, time stamp/time frame, and one ore more participating Users. A user can create one ore more activity and by the same token the owners of Activity will be considered to own the Event.