Grails Many to one Relationship deletion - grails

class First {
String text
Second second
static constraints = {
}
}
class Second {
String name
static constraints = {
}
}
When I delete Second class object, I got an error like this:
Cannot delete or update a parent row: a foreign key constraint fails.
I want to delete only the instance of Second inside First.

You have to remove the association of Second from First (aka FK constraint) and then will be able to delete Second.
first.second = null
second.delete()
Refer removeFrom for more details when one-many & many-many relations are used.

Related

Copy fields between similar classes swift

I have two classes that are same but are present in different places. One inside framework and one in Normal App classes. I want to copy all the fields between them.
class Account {
var prodCode;
var subProdCode;
}
Same class inside the framework.
class Account {
var prodCode;
var subProdCode;
}
I need all fields to be copied from Object of 1st class to Object second class.
What is the best and easy way to do in Swift?
Add a method to MyAccout to update values of properties in your class, which accepts FrameworkAccount as parameter.
class MyAccout {
//...
func updateValues(account: FrameworkAccount) {
prodCode = account.prodCode
subProdCode = account.subProdCode
}
}

Obtaining inverse relationships from a generic List in Realm

I would like to create a generic Realm List consisting of different class types (but all of superclass Object).
class Parent: Object {
var children = List<Object>()
}
class Child1: Object {
let parents = LinkingObjects(fromType: Parent.self, property: "children")
}
class Child2: Object {
let parents = LinkingObjects(fromType: Parent.self, property: "children")
}
Linking instances of Child1 and Child2 to a Parent object works fine, however, the inverse relationships gives the following error:
- Property ‘Parent.children’ declared as origin of linking objects property ‘Child1.parents’ links to a different class.
- Target type 'RealmSwiftObject' doesn't exist for property ‘children’.
How could it be ensured that the parents property is determined correctly? I can imagine that it should be filtered for the class type where it belongs to (i.e. Child1 or Child2) but I don't know how to do that?
Unless there is a workaround I am not aware of, it is not possible to store different types in a List.
Lists contain other Objects of a single type and have an interface very similar to a mutable Array. (https://realm.io/docs/swift/latest/#to-many-relationships)

add constraint to plugin domain class

In order to support UTFMB8 encoding I added the following default constraint in Config.groovy
grails.gorm.default.constraints = {
'*'(maxSize: 191)
unlimitedSize(maxSize: Integer.MAX_VALUE)
}
I also added the unlimitedSize shared constraint which I use in some of my domain classes to override this default, e.g.
class BlogPost {
String body
static constraints = {
body shared: 'unlimitedSize'
}
}
However, there are a couple of classes in plugins that also need to override the default maxSize of 191. In these cases, I can't use the shared constraint, because I can't edit the source code. One option is to copy the classes into my application, and edit the copies (because artifacts in an application override those in plugins), but this is not very appealing because I've then effectively forked those classes.
Is there a better way? For example, is it possible for me to add constraints to these domain classes in Bootstrap.groovy?
Constraints can be added during boostrap something like:
class BootStrap {
def grailsApplication
def init = { servletContext ->
grailsApplication.domainClasses.each { GrailsDomainClass gdc ->
Class domainClass = gdc.clazz
if (domainClass.simpleName == 'BookFromPlugin') {
def field = domainClass.declaredFields.find {
it.name == 'body' && it.type == String
}
if (field) {
domainClass.constraints.body.maxSize = Integer.MAX_VALUE
}
}
}
}
}
where BookFromPlugin is a domain class from plugin and body is a property from the domain class. This can be optimized and made applicable to more than one domain class.
Since bootstrap is the last thing that is taken care of, it should eventually override the constraint previously defined in domain class.
UPDATE:
I guess you meant domainClass.constraints is accessing the private variable from domain class, but that is not true. domain.constraints gives a map where propery name is mapped to all its constraints. This is map is taken from ConstrainedProperty which is comprised of 3 elements: class owning the constraint, property name where constraint will be applied and the type of the property.
So when we use domain.constraints.body it actually gives a list of constraints applied to body as the value for key body. Each element in the list is a ConstrainedProperty.
By calling setMaxSize() we are just adding another ConstrainedProperty to the list of constraints.

Having one class in one of two one-to-many relationships (eg: Comments on Video OR Image) in Grails?

Im building an example where there exists Video and Image domain models. Each have a one-to-many relationship with the Comment model as follows:
package commentstest
class Video {
static constraints = {
embeddUrl(blank:false,nullable:false,url:true)
}
String embeddUrl
static hasMany = [comments:Comment]
}
and
package commentstest
class Image {
static constraints = {
fileName(blank:false,nullable:false)
}
String fileName
static hasMany = [comments: Comment]
}
and finally the Comment class
package commentstest
class Comment {
static constraints = {
body(blank:false,nullable:false)
}
String body
static belongsTo = [image:Image, video:Video]
}
Now the problem i'm having is that when i create a 'comment' it must be able to be added to either a video OR an image. Currently, the scaffolding populates both the image and video option, and doesnt give the option to leave empty for one of those fields.
Anyone have an idea how to do this? Im sure my problems are spawning from the line:
static belongsTo = [image:Image,video:Video]
But i dont know how to specify that it must belong to one OR the other. not both.
From what I could see and understand, it's like you saystatic belongsTo = [image:Image,video:Video] is the root cause.
What you should do is add something like this to your constraints in Comment
static constraints = {
fileName(blank:false,nullable:false)
image(nullable:true)
video(nullable:true)
}
That enables you to set it to either a video or image.
Hope it hepls!

Has-A Relationship

public class Elevator ()
{
Button firstFloorbutton = ButtonFactory.getButtonInstance(this, 1);
Button secondFloorbutton = ButtonFactory.getButtonInstance(this, 2);
Button thirdFloorbutton = ButtonFactory.getButtonInstance(this, 3);
Button fourthFloorbutton = ButtonFactory.getButtonInstance(this, 4);
Fan fan1 = FanFactory.getFanInstance(this);
Light light1 = LightFactory.getInstance(this);
private void goUp()
{
.....
}
private void goDown()
{
......
}
.............
}
============================
public class Button()
{
Elevator E;
int floor;
public button (Elevator E, int floor )
{
this.E = E;
this.floor = floor;
}
public void buttonPressed()
{
//logic to determine which floor the elevator is currently at and determine whether to go up or down to reach "this.floor"
E.goUp(); // if need to go up
else
E.goDown() // if need to go down
}
}
==========================
public class ButtonFactory()
{
public Button getButtonInstance(Elevator E, int floor)
{
Button b =new Button(E, floor);
return b;
}
}
==================
public class FanFactory(){ .................}
=====================
public class LightFactory() { ........... }
==========================
What kind of relationship exist between the Elevator and Button class?
According to Kathy and Bert (SCJP) page 92 : HAS-A relationship are based on usage rather than inheritance. In other words, class A HAS-A B if code in class A has a reference to an instance of class B.
In my example Elevator class code have a reference to a instance of Button and Button have a reference to instance of Elevator class.
Can anyone please clarify on this.
The Elevator has a button. Actually, it has four, but with each of those buttons, there's a has-a relationship.
Has-a is a somewhat informal term used to refer to two more formal kinds of relationship: association and aggregation. In both cases, one party in the relationship has a pointer to the other, but they're distinguished by semantics: in an association relationship, the first party knows about the other, but doesn't completely dominate it (think you and a colleague, or a boss, or a subordinate), whereas in an aggregation relationship, the latter party is part of the former party, and has no independent existence (think you and your liver). In this case, i'd say the Button is more specifically on the subordinate end of an aggregation relationship with the Elevator, not merely an association relationship.
Other examples of association might be a Customer and a Salesman, or a Department and an Employee. Of aggregation, an Order and and OrderLine, or a Structure and a Component. Interesting corner cases are a Category and a Product, and an Order and an Invoice.
One practical consequence of the kind of relationship is object lifetime: in an association, if the first object dies, the second may live, but in an aggregation, it will die. Think about your Elevator: if you deleted one (or removed it from your live data structures and let it be garbage collected, at least), would you want the Buttons to survive?
It is a Has-A relationship, which is a simple way of remember the composition model. Button class is composed of Elevator class; i.e. Button class has a Elevator class.

Resources