-
1. Re: Problem with injection @In
matt.drees Oct 5, 2007 12:28 AM (in response to luiz.sg)Which part of the hibernate example are you referring to?
-
2. Re: Problem with injection @In
luiz.sg Oct 5, 2007 12:48 AM (in response to luiz.sg)"matt.drees" wrote:
Which part of the hibernate example are you referring to?
This one:@Entity @Name("user") @Scope(SESSION) @Table(name="Users") public class User implements Serializable { private String username; private String password; private String name; public User(String name, String password, String username) { this.name = name; this.password = password; this.username = username; } public User() {} @NotNull @Length(max=100) public String getName() { return name; } public void setName(String name) { this.name = name; } @NotNull @Length(min=5, max=15) public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Id @Length(min=5, max=15) public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } @Override public String toString() { return "User(" + username + ")"; } }
The POJO:@Scope(SESSION) @Name("bookingList") public class BookingListAction implements Serializable { @In private Session bookingDatabase; @In private User user; @DataModel private List<Booking> bookings; @DataModelSelection private Booking booking; @Factory("bookings") public void find() { bookings = bookingDatabase.createQuery("from Booking b where b.user = :user order by b.checkinDate") .setParameter("user", user) .list(); } public String cancel() { Booking cancelled = (Booking) bookingDatabase.get(Booking.class, booking.getId()); if (cancelled!=null) bookingDatabase.delete( cancelled ); refresh(); return "cancelled"; } public void refresh() { booking = null; find(); } }
Another one:@Scope(EVENT) @Name("register") public class RegisterAction { @In private User user; @In private Session bookingDatabase; @In private FacesMessages facesMessages; private String verify; public String register() { if ( user.getPassword().equals(verify) ) { List existing = bookingDatabase.createQuery("select username from User where username=:username") .setParameter("username", user.getUsername()) .list(); if (existing.size()==0) { bookingDatabase.persist(user); return "login"; } else { facesMessages.add("username already exists"); return null; } } else { facesMessages.add("re-enter your password"); verify=null; return null; } } public String getVerify() { return verify; } public void setVerify(String verify) { this.verify = verify; } }
I tried to put my components in the SESSION scope, lilke in the example, but still don't work.
Am I missing some configuration? -
3. Re: Problem with injection @In
saeediqbal1 Oct 5, 2007 12:57 AM (in response to luiz.sg)exactly what was causing me alot of waste of time and frustration. Something that works in the examples should also work in our projects. whats up with that?
-
4. Re: Problem with injection @In
swd847 Oct 5, 2007 1:10 AM (in response to luiz.sg)Are you referring to the booking example?
The reason why it works in the example is because of this little bit of code in the AuthenticatorBean:
@Out(required=false, scope = SESSION)
private User user;
when a user logs in the user entity is outjected to session scope, allowing it be used by other beans. When you think about it it makes sense that an exception is thrown, how can you inject the current user if they have not logged in yet?
@In without create=true is basically saying that you want an initialized User injected, with create=true you are saying that you do not care if it has been initialized or not, you just want a user. -
5. Re: Problem with injection @In
nickarls Oct 5, 2007 1:12 AM (in response to luiz.sg)Well, as I've understood it, Seam creates non-existing components as they are referenced. So if you inject A into B it might work fine if you have referenced A earlier in the page.
I don't think session scope is any different, if you only reference it late in the page it doesn't exist before that. -
6. Re: Problem with injection @In
luiz.sg Oct 5, 2007 1:17 AM (in response to luiz.sg)"saeediqbal1" wrote:
exactly what was causing me alot of waste of time and frustration. Something that works in the examples should also work in our projects. whats up with that?
What a I don't undestand is that I can run the example perfetly, and I based my projetc in this example.
Hope someone here can see what I couldn't. -
7. Re: Problem with injection @In
luiz.sg Oct 5, 2007 1:21 AM (in response to luiz.sg)"swd847" wrote:
Are you referring to the booking example?
The reason why it works in the example is because of this little bit of code in the AuthenticatorBean:
@Out(required=false, scope = SESSION)
private User user;
when a user logs in the user entity is outjected to session scope, allowing it be used by other beans. When you think about it it makes sense that an exception is thrown, how can you inject the current user if they have not logged in yet?
@In without create=true is basically saying that you want an initialized User injected, with create=true you are saying that you do not care if it has been initialized or not, you just want a user.
Ok. It makes sense now...
Tanks for the explanation. -
8. Re: Problem with injection @In
luiz.sg Oct 5, 2007 2:02 AM (in response to luiz.sg)"swd847" wrote:
when a user logs in the user entity is outjected to session scope, allowing it be used by other beans. When you think about it it makes sense that an exception is thrown, how can you inject the current user if they have not logged in yet?
Yes, I tought i had understood but....
Why an exception is not thrown in the register page if you didn't passed by the Authenticator yet?
I'm confused.