Hi I am using Spring Boot app with MVC. I have two model classes Parent and Child. Parent to Child has OnetoMany Mapping. I have a form submit in which I'm passing few fields of child class, now what is happening when I bind the child class in the controller method is that it picks up the id of Parent class and binds it automatically to Child object in Controller.Please suggest what I am missing in here.
Following is the code for the same:
#PostMapping("/{id}/child")
public String editChildPOST(#PathVariable Long id,
Child child, Model model, BindingResult errors,
Principal principal) {
logger.info("editChildPOST: " + child.getId);
model.addAttribute("aaaa", aaaa);
model.addAttribute("bbbb", "bbbb");
return "redirect:/xxxx";
}
Following is the mapping in the parent Class :
class Parent{
#OneToMany(cascade= ALL, orphanRemoval=true)
#JoinColumn(name="parent_id")
private List<Child> child;
}
SO in debug mode if I try to check the value of child in Controller it shows Child Id as parent's Id although I haven't set Child Id anywhere in the form submit.
Related
I need to change the visibility of a button deppending on a recycler's view content, when it changes it must become visible otherwise it has to remain invisible
I've tried with interfaces, callback and "onactivityresult" to add a changelistener to the recyclerview, with the interface I can access a method from the parent class but I cant reach the view's it returns me nulls
the structure is the following:
parent class(viewpager, with button that needs to change it's visibility)
child class(recyclerview)
adapter(Call to API service, listener to button and Edittext, "I need to modify it from here")
Here are the steps if you want to send data from child class to parent class.
You have to create a custom listener.
public interface HideButton{
void hideButton();
}
now create a instance of this interface in child class or adapter class
HideButton hideButtonListener;
and create a setter in the adapter class,
public void setButtonHideListener(HideButton listener){
this.hideButtonListener = listener;
}
and call this setter from parent class by the adapter reference e.g
adapter.setButtonHideListener(new HideButton(){
void hideButton(){
//here you change the visibility of the button in parent class
}
});
one more step. Now when you call an api in adapter and receive the response, you call this code
if(hideButtonListener != null){
hideButtonListener.hideButton();
}
Suppose we have the following entities:
#Entity
#Cache
public class Site{
....
}
#Entity
#Cache
public class Page{
#Parent
private Key<Site> parent;
}
#Entity
#Cache
public class Comment{
#Parent
private Key<Page> parent;
}
What is the best way to query after the list of comments of a Page, having the Page id and Site id? I have tried .ancestor() method but this only accepts the Site entity,throwing a
java.lang.NullPointerException
at com.googlecode.objectify.impl.Keys.getMetadataSafe(Keys.java:61)
if I supply a Page entity instead. ancestor (site) is not good enough for this case, as I need the comments for a specific page. The only solution I can think about is to add a new attribute in the Comment entity Long pageId and #Index it and then use filter to get the comments of a page. Is there a better solution? Thank you!
Consider the following:
Public class Foo()
{
public BarClass Bar = new BarClass();
}
Here we have a simple class called Foo. Bar is a property of Foo of type BarClass. So when iterating through the code elements of Foo. Bar is simply a property and has no children.
However we do have access to the fullname of Bar.
How can I create a new CodeElement instance using the FullName property, so I can have access to the properties and methods (via the children collection) of Bar?
foreach (CodeElement child in Foo)
{
// Bar shows up as a property, and you can access the Fullname here...
}
Your question is not 100% clear (Bar is not a property of Foo, it is a field, and has no children). But to navigate the code elements using the automation model (EnvDTE) you need to cast EnvDTE.CodeElement to a specific type (such as EnvDTE.CodeClass, or EnvDTE.CodeType) and use the Members property, not the Children property. See: HOWTO: Navigate the code elements of a file from a Visual Studio .NET macro or add-in
Since the Entity Framework creates proxy instead of providing the "original" entity classes, how do you cast a parent class to a child class?
This does not work "the normal way" because the automatically created proxy classes don't use the inheritance structure of the original entity classes.
Turning off the proxy-creation feature is not an option for me.
Any help is welcome, thanks!
How do you cast a parent class to a child class?
You can only cast a parent to a child if the actual runtime type is a child. That's true for non-proxies and proxies because a proxy of a child derives from a child, hence it is a child. A proxy of a parent is not a child, so you can't cast it to a child, neither can you cast a parent to a child.
For example (using DbContext API):
public class Parent { ... }
public class Child : Parent { ... }
Then both the following casts will work:
Parent parent1 = context.Parents.Create<Child>(); // proxy
Parent parent2 = new Child(); // non-proxy
Child child1 = (Child)parent1; // works
Child child2 = (Child)parent2; // works
And both the following won't work:
Parent parent1 = context.Parents.Create<Parent>(); // proxy
Parent parent2 = new Parent(); // non-proxy
Child child1 = (Child)parent1; // InvalidCastException
Child child2 = (Child)parent2; // InvalidCastException
Both casts work "the normal way".
I am working in air application , i need to know how to add event listener when any object is updated, how can i implement this.
Example: i have Class name Vehicle, and child class are Car,Bus,Bikes,Scooter,..etc, child class also have many properties like color,model no,....etc
I have array collection and AddChild() method in Vehicle class by this, i will add, child class to the vehicle class.
I need a event listener which can trigger if any of the property is updated or changed in any of the child class property, how can i implements this in Flex3.
I need this for knowing, is there any update happen in the Object.
Thanks In Advance
One thing you can do is to make getters and setters for the properties instead of public vars and have your class extend EventDispatcher, if it does not already do so because it is extended from a MovieClip, like:
private var _vehicleName:String;
.
.
.
public function set vehicleName(value:String):void {
_vehicleName = value;
dispatchEvent(new VehicleEvent(VehicleEvent.propertyChange, "vehicleName"));
}
public function get vehicleName():String {
return _vehicleName;
}
(VehicleEvent being an extended class of Event with an extra string to signify which property changed)
Then you can add an eventlistener to the vehicles and they will dispatch events when the properties defined in this way change.
If you use an Array Collection for the vehicles and if you make the properties of the vehicles Bindable as Amarghosh suggests, then the array collection already should throw an Collection Event of the kind UPDATE. It also tells you which items (Vehicles) were updated an afaik also which properties were updated. But in general it is easier to directly bind to the property, like Amarghosh says.