-
1. Re: constructor not called of embeeded objects
pmuir Sep 6, 2010 6:06 AM (in response to nimo22)Do you @Inject Role into Users?
-
2. Re: constructor not called of embeeded objects
nimo22 Sep 6, 2010 6:43 AM (in response to nimo22)No, I did not @Inject Role into Users.
That was my fault.
I have thought, weld creates (inject) new instances of objects automatically.So I have to use this code (both versions work):
version 1:
public class User implements Serializable{ ... private @Inject Role role; // getter/setter }
version 2:
public class User implements Serializable{ ... private Role role = new Role(); // getter/setter }
I guess, I will use version 2 as these objects are pojos (entitybeans).
-
3. Re: constructor not called of embeeded objects
nimo22 Sep 6, 2010 9:44 AM (in response to nimo22)Another thing is:
There are cases, I do need a new Role-Object when instantiating a new User-Object.
But there are also cases, I do not need a new Role-Object - so (for a big project), it is a waste to create a new Instance without needing it. I can use a NullObjectPattern, but this is annoying.Should I do it that way:
public class User implements Serializable{ ... // this is actually null, // I should avoid using Role role = new Role() or @Inject Role role // because it will be a waste, if I do not need it. private Role role; // getter/setter } public class MyBean implements Serializable{ ... @Inject User u; @PostConstruct public void init() { // this is a little annoying: // I can eliminate that boilerplate-code by creating a new instance of role // within a user-class, but this would be a waste if I do not need a role-instance u.setRole(new Role()); } }
So, how can I solve that in a pretty way?
When using @Inject role, than weld does also create a new Instance within the class User even I do not need it.Does Weld @Inject handles the NullObjectPattern internally?