Hi,
A fellow explained me the questions, so it let me develop a simple example.
It has two entities, a stateless session as facade and the app client.
Every entity map a table.
Here is the code,
1. package entity;
2.
3. import java.io.Serializable;
4. import javax.persistence.*;
5. import static javax.persistence.CascadeType.*;
6. import java.util.Collection;
7. import java.util.ArrayList;
8.
9. /**
10. *
11. * @author NetbeansUser
12. */
13. @Entity
14. public class Customer implements Serializable {
15. private int id;
16. private String name;
17. private Collection<Order> orders = new ArrayList<Order>();
18.
19. @Id
20. public int getId() {
21. return id;
22. }
23.
24. public void setId(int id) {
25. this.id = id;
26. }
27.
28. public String getName() {
29. return name;
30. }
31.
32. public void setName(String name) {
33. this.name = name;
34. }
35.
36. @OneToMany(cascade=ALL, mappedBy="customer")
37. public Collection<Order> getOrders() {
38. return orders;
39. }
40.
41. public void setOrders(Collection<Order> newValue) {
42. this.orders = newValue;
43. }
44.
45. @Override
46. public int hashCode() {
47. int hash = 0;
48. hash += (int) id;
49. return hash;
50. }
51.
52. @Override
53. public boolean equals(Object object) {
54. // TODO: Warning - this method won't work in the case the id fields are not set
55. if (!(object instanceof Customer)) {
56. return false;
57. }
58. Customer other = (Customer) object;
59. if (this.id != other.id) {
60. return false;
61. }
62. return true;
63. }
64.
65. @Override
66. public String toString() {
67. return "entity.Customer[id=" + id + "]";
68. }
69.
70. }
--------------------------------------------------------------------------------
1. package entity;
2.
3. import javax.persistence.*;
4.
5. /**
6. *
7. * @author NetbeansUser
8. */
9. @Entity
10. @Table(name="ORDER_TABLE")
11. public class Order {
12.
13. private int id;
14. private String address;
15. private Customer customer;
16.
17. @Id
18. @Column(name="ORDER_ID")
19. public int getId() {
20. return id;
21. }
22.
23. public void setId(int id) {
24. this.id = id;
25. }
26.
27. @Column(name="SHIPPING_ADDRESS")
28. public String getAddress() {
29. return address;
30. }
31.
32. public void setAddress(String address) {
33. this.address = address;
34. }
35.
36. @ManyToOne()
37. @JoinColumn(name="CUSTOMER_ID")
38. public Customer getCustomer() {
39. return customer;
40. }
41.
42. public void setCustomer(Customer customer) {
43. this.customer = customer;
44. }
45. }
-----------------------------------------------------------------------------
# package ejb;
#
# import javax.ejb.Remote;
# import entity.Customer;
#
# /**
# *
# * @author NetbeansUser
# */
# @Remote
# public interface Test {
# // Insert Customer and Orders
# public String testInsert();
#
# // Verify that all are inserted
# public String verifyInsert();
#
# // Remove Customer and Orders
# public String testDelete(int id);
#
# // Verify that all are removed
# public String verifyDelete();
#
# // Get a detached instance of a Customer
# public int findCustomer(String name);
#
# public Customer findCustomer(int id);
# }
----------------------------------------------------------------------------------
1. package ejb;
2.
3. import javax.ejb.Stateless;
4. import javax.persistence.EntityManager;
5. import javax.persistence.PersistenceContext;
6. import javax.persistence.Query;
7.
8. import java.util.Collection;
9. import java.util.List;
10.
11. import entity.*;
12.
13. /**
14. *
15. * @author NetbeansUser
16. */
17. @Stateless(name="ejb/Test")
18. public class TestBean implements Test {
19.
20. // Add business logic below. (Right-click in editor and choose
21. // "Insert Code > Add Business Method" or "Web Service > Add Operation")
22. @PersistenceContext(unitName="pu1")
23. private EntityManager em;
24.
25. public String testInsert() {
26.
27. // Create new customer
28. Customer customer0 = new Customer();
29. customer0.setId(1);
30. customer0.setName("Joe Smith");
31.
32. // Persist the customer
33. em.persist(customer0);
34.
35. // Create 2 orders
36. Order order1 = new Order();
37. order1.setId(100);
38. order1.setAddress("123 Main St. Anytown, USA");
39.
40. Order order2 = new Order();
41. order2.setId(200);
42. order2.setAddress("567 1st St. Random City, USA");
43.
44. // Associate orders with the customer. The association
45. // must be set on both sides of the relationship: on the
46. // customer side for the orders to be persisted when
47. // transaction commits, and on the order side because it
48. // is the owning side.
49. customer0.getOrders().add(order1);
50. order1.setCustomer(customer0);
51.
52. customer0.getOrders().add(order2);
53. order2.setCustomer(customer0);
54.
55. return "OK";
56. }
57.
58. public String verifyInsert() {
59.
60. int id = findCustomer("Joe Smith");
61.
62. Query q = em.createQuery("select c from Customer c where c.id = :id");
63. q.setParameter("name", id);
64. Customer c = (Customer)q.getSingleResult();
65.
66. Collection<Order> orders = c.getOrders();
67. if (orders == null || orders.size() != 2) {
68. throw new RuntimeException("Unexpected number of orders: "
69. + ((orders == null)? "null" : "" + orders.size()));
70. }
71.
72. return "OK";
73. }
74.
75. public String testDelete(int id) {
76.
77. Customer c = findCustomer(id);
78.
79. // Merge the customer to the new persistence context
80. Customer c0 = em.merge(c);
81.
82. // Delete all records.
83. em.remove(c0);
84.
85. return "OK";
86. }
87.
88. public String verifyDelete() {
89.
90. Query q = em.createQuery("select c from Customer c");
91. List results = q.getResultList();
92.
93. if (results == null || results.size() != 0) {
94. throw new RuntimeException("Unexpected number of customers after delete");
95. }
96.
97. q = em.createQuery("select o from Order o");
98. results = q.getResultList();
99.
100. if (results == null || results.size() != 0) {
101. throw new RuntimeException("Unexpected number of orders after delete");
102. }
103.
104. return "OK";
105. }
106.
107. public int findCustomer(String name) {
108.
109. Query q = em.createQuery("select c from Customer c where c.name = :name");
110. q.setParameter("name", name);
111. Customer c = (Customer)q.getSingleResult();
112. return c.getId();
113. }
114.
115. public Customer findCustomer(int id){
116.
117. Query q = em.createQuery("select c from Customer c where c.id = :id");
118. q.setParameter("id", id);
119. return (Customer)q.getSingleResult();
120. }
121. }
----------------------------------------------------------------------------
and the client
1. package client;
2.
3. import javax.ejb.*;
4. import ejb.Test;
5.
6. /**
7. * @author NetbeansUser
8. */
9. public class AppClient {
10.
11. @EJB(name="ejb/Test")
12. private static Test sb;
13.
14. public static void main(String[] args) {
15. AppClient test = new AppClient();
16. test.runTest();
17. }
18.
19.
20. public void runTest() {
21.
22. // Persist all entities
23. System.out.println("Inserting Customer and Orders... " + sb.testInsert());
24.
25. // Test query and navigation
26. System.out.println("Verifying that all are inserted... " + sb.verifyInsert());
27.
28. // Get a detached instance
29. int id = sb.findCustomer("Joe Smith");
30.
31. // Remove all entities
32. System.out.println("Removing all... " + sb.testDelete(id));
33.
34. // Query the results
35. System.out.println("Verifying that all are removed... " + sb.verifyDelete());
36. }
37. }
As you can see it is based on an example form java.net but i tried to separate the entities of the client leaving all the busines logic to the facade.
But it does not work. I get a NullPointerException
in the call to the facade in the client.
Here is the stacktrace,
1. 19-feb-2009 11:46:36 com.sun.enterprise.appclient.MainWithModuleSupport <init>
2. ADVERTENCIA: ACC003: La aplicación desencadenó una excepción.
3. java.lang.NullPointerException
4. at client.AppClient.runTest(AppClient.java:28)
5. at client.AppClient.main(AppClient.java:21)
6. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
7. at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
8. at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
9. at java.lang.reflect.Method.invoke(Method.java:597)
10. at com.sun.enterprise.util.Utility.invokeApplicationMain(Utility.java:266)
11. at com.sun.enterprise.appclient.MainWithModuleSupport.<init>(MainWithModuleSupport.java:449)
12. at com.sun.enterprise.appclient.MainWithModuleSupport.<init>(MainWithModuleSupport.java:259)
13. at com.sun.enterprise.appclient.Main.main(Main.java:200)
14. Exception in thread "main" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
15. at com.sun.enterprise.appclient.MainWithModuleSupport.<init>(MainWithModuleSupport.java:461)
16. at com.sun.enterprise.appclient.MainWithModuleSupport.<init>(MainWithModuleSupport.java:259)
17. at com.sun.enterprise.appclient.Main.main(Main.java:200)
18. Caused by: java.lang.reflect.InvocationTargetException
19. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
20. at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
21. at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
22. at java.lang.reflect.Method.invoke(Method.java:597)
23. at com.sun.enterprise.util.Utility.invokeApplicationMain(Utility.java:266)
24. at com.sun.enterprise.appclient.MainWithModuleSupport.<init>(MainWithModuleSupport.java:449)
25. ... 2 more
26. Caused by: java.lang.NullPointerException
27. at client.AppClient.runTest(AppClient.java:28)
28. at client.AppClient.main(AppClient.java:21)
29. ... 8 more
Any idea?
Best regards,
Jose Alvarez de Lara
[Message sent by forum member 'josealvarezdelara' (josealvarezdelara)]
http://forums.java.net/jive/thread.jspa?messageID=332875