erreur incompréhensible : java.lang.reflect.UndeclaredThrowableException
salut,
j'ai cette erreur incompréhensible pour moi, je vais essayer de décrire mon projet :
il s'agit de l'exemple du livre d'A.Goncalves sur jee5.
le projet est constitué de classes JPA, d'EJBs pour accéder à ces entities JPA, et d'un projet java contenant une classe main censée stocker une instance de l' entity "category", et de la retrouver ensuite.
voici quelques détails :
Category.java
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
|
package entities;
import java.io.Serializable;
import java.util.List;
import javax.persistence.*;
/**
* Entity implementation class for Entity: Category
*
*/
@Entity
@Table(name="t_category")
public class Category implements Serializable {
private static final long serialVersionUID = 1L;
public Category() {
super();
}
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
@Column(nullable=false,length=30)
private String name;
@Column(nullable=false)
private String description;
@OneToMany(mappedBy="category",
cascade=CascadeType.REMOVE,
fetch=FetchType.LAZY)
@OrderBy("name ASC")
private List<Product> products;
@PrePersist
@PreUpdate
private void validateData(){
if (name==null || "".equals(name))
throw new ValidationException("Invalid name");
if (description==null || "".equals(description))
throw new ValidationException("Invalid description");
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public List<Product> getProducts() {
return products;
}
public void setProducts(List<Product> products) {
this.products = products;
}
(...) |
persistence.xml
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
|
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="jpa3">
<jta-data-source>java:MySqlDS</jta-data-source>
<class>entities.Category</class>
<class>entities.Product</class>
<class>entities.Item</class>
<class>entities.Customer</class>
<class>entities.Address</class>
<class>entities.Order</class>
<class>entities.OrderLine</class>
<class>entities.CreditCard</class>
<properties>
<property name="eclipselink.ddl-generation" value="drop-and-create-tables" />
<property name="eclipselink.ddl-generation.output-mode"
value="database" />
<property name="eclipselink.target-database" value="MySQL" />
<property name="eclipselink.logging.level" value="INFO" />
<!-- <property name="eclipselink.jdbc.url" value="jdbc:mysql://localhost:3306/base2"/>-->
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
</properties>
</persistence-unit>
</persistence> |
mysql-ds.xml (pour jboss, c'est la datasource)
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
|
<?xml version="1.0" encoding="UTF-8"?>
<!-- $Id: mysql-ds.xml 41017 2006-02-07 14:26:14Z acoliver $ -->
<!-- Datasource config for MySQL using 3.0.9 available from:
http://www.mysql.com/downloads/api-jdbc-stable.html
-->
<datasources>
<local-tx-datasource>
<jndi-name>MySqlDS</jndi-name>
<connection-url>jdbc:mysql://localhost:3306/base2</connection-url>
<driver-class>com.mysql.jdbc.Driver</driver-class>
<user-name>root</user-name>
<password>root</password>
<exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter</exception-sorter-class-name>
<!-- should only be used on drivers after 3.22.1 with "ping" support
<valid-connection-checker-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLValidConnectionChecker</valid-connection-checker-class-name>
-->
<!-- sql to call when connection is created
<new-connection-sql>some arbitrary sql</new-connection-sql>
-->
<!-- sql to call on an existing pooled connection when it is obtained from pool - MySQLValidConnectionChecker is preferred for newer drivers
<check-valid-connection-sql>some arbitrary sql</check-valid-connection-sql>
-->
<!-- corresponding type-mapping in the standardjbosscmp-jdbc.xml (optional) -->
<metadata>
<type-mapping>mySQL</type-mapping>
</metadata>
</local-tx-datasource>
</datasources> |
Catalog.java (un des beans, dans le projet EJB)
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
|
package ejbs;
import java.util.List;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import entities.Category;
import entities.Item;
import entities.Product;
/**
* Session Bean implementation class CatalogBean
*/
@Stateless(name = "CatalogSB", mappedName = "ejb/stateless/Catalog")
@TransactionAttribute(value=TransactionAttributeType.REQUIRED)
public class CatalogBean implements CatalogRemote, CatalogLocal {
@PersistenceContext(unitName="jpa3")
private EntityManager em;
/**
* Default constructor.
*/
public CatalogBean() {
// TODO Auto-generated constructor stub
}
@Override
public List<Item> searchItems(String keyword) {
Query query;
List<Item> items;
query=em.createQuery("select i from Item i where "+
"upper(i.name) like :keyword or "+
"upper(i.product.name) like :keyword "+
"order by i.product.category.name, i.product.name");
query.setParameter("keyword", "%"+keyword.toUpperCase()+"%");
items=query.getResultList();
return items;
}
@Override
public Category createCategory(Category category) {
if (category==null)
throw new ValidationException("create category impossible : category is null");
em.persist(category);
return category;
}
@Override
public Category findCategory(Long categoryId) {
if (categoryId==null)
throw new ValidationException("findCategory impossible since categoryId is null");
return em.find(Category.class, categoryId);
}
@Override
public void deleteCategory(Category category) {
if (category==null)
throw new ValidationException("deleteCategory is impossible since category is null");
em.remove(em.merge(category));
}
@Override
public Category updateCategory(Category category) {
if (category==null)
throw new ValidationException("update category is impossible since category is null");
return em.merge(category);
}
@Override
public List<Category> findCategories() {
Query query;
List<Category> categories;
query=em.createQuery("select c from Category c");
categories=query.getResultList();
return categories;
}
@Override
public Product createProduct(Product product, Category category) {
if (product==null)
throw new ValidationException("createProduct impossible since the product is null");
product.setCategory(category);
em.persist(product);
return product;
}
@Override
public Product findProduct(Long productId) {
if (productId==null)
throw new ValidationException("findProduct impossible since productId is null");
return em.find(Product.class, productId);
}
@Override
public void deleteProduct(Product product) {
if (product==null)
throw new ValidationException("deleteProduct is impossible since the product is null");
em.remove(em.merge(product));
}
@Override
public Product updateProduct(Product product, Category category) {
if (product==null)
throw new ValidationException("update product is impossible since the product is null");
product.setCategory(category);
return em.merge(product);
}
@Override
public List<Product> findProducts() {
Query query;
List<Product> products;
query=em.createQuery("select p from Product p");
products=query.getResultList();
return products;
}
@Override
public Item createItem(Item item, Product product) {
if (item==null)
throw new ValidationException("create item is impossible since the item is null");
item.setProduct(product);
em.persist(item);
return item;
}
@Override
public Item findItem(Long itemId) {
if (itemId==null)
throw new ValidationException("findItem impossible since the itemId is null");
return em.find(Item.class, itemId);
}
@Override
public void deleteItem(Item item) {
if (item==null)
throw new ValidationException("deleteItem is impossible since the item is null");
em.remove(em.merge(item));
}
@Override
public Item updateItem(Item item, Product product) {
if (item==null)
throw new ValidationException("update item is impossible since the item is null");
item.setProduct(product);
return em.merge(item);
}
@Override
public List<Item> findItems() {
Query query;
List<Item> items;
query=em.createQuery("select i from Item i");
items=query.getResultList();
return items;
}
} |
catalogDelegate.java(design pattern, j'ai écris ça à partir du bouquin mais je n'ai pas tout compris)(ça concerne les 2 fichiers que voici)
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
|
package access_jpa;
import java.util.List;
import ejbs.CatalogRemote;
import entities.Category;
import entities.Item;
import entities.Product;
public final class CatalogDelegate {
// ======================================
// = Category Business methods =
// ======================================
public static Category createCategory(Category category) {
Category cat=null;
try {
cat=getCatalogRemote().createCategory(category);
} catch (Exception e) {
System.out.println(e);
}
return cat;
}
public static Category findCategory(Long categoryId) {
return getCatalogRemote().findCategory(categoryId);
}
public static void deleteCategory(Category category) {
getCatalogRemote().deleteCategory(category);
}
public static Category updateCategory(Category category) {
return getCatalogRemote().updateCategory(category);
}
public static List<Category> findCategories() {
List<Category> liste=null;
try {
liste= getCatalogRemote().findCategories();
} catch (Exception e) {
System.out.println(e);
}
return liste;
}
// ======================================
// = Product Business methods =
// ======================================
public static Product createProduct(Product product, Category category) {
return getCatalogRemote().createProduct(product, category);
}
public static Product findProduct(Long productId) {
return getCatalogRemote().findProduct(productId);
}
public static void deleteProduct(Product product) {
getCatalogRemote().deleteProduct(product);
}
public static Product updateProduct(Product product, Category category) {
return getCatalogRemote().updateProduct(product, category);
}
public static List<Product> findProducts() {
return getCatalogRemote().findProducts();
}
// ======================================
// = Item Business methods =
// ======================================
public static Item createItem(Item item, Product product) {
return getCatalogRemote().createItem(item, product);
}
public static Item findItem(Long itemId) {
return getCatalogRemote().findItem(itemId);
}
public static void deleteItem(Item item) {
getCatalogRemote().deleteItem(item);
}
public static Item updateItem(Item item, Product product) {
return getCatalogRemote().updateItem(item, product);
}
public static List<Item> findItems() {
return getCatalogRemote().findItems();
}
// ======================================
// = Private methods =
// ======================================
private static CatalogRemote getCatalogRemote() {
CatalogRemote catalogRemote=null;
try {
catalogRemote = (CatalogRemote) ServiceLocator.getInstance().getRemoteInterface("ejb/stateless/Catalog");
} catch (Exception e) {
System.out.println(e);
}
return catalogRemote;
}
} |
serviceLocator.java (le 2e fichier)
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
|
package access_jpa;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import javax.naming.Context;
import javax.naming.InitialContext;
public class ServiceLocator {
private Context initialContext;
private Map<String, Object> cache;
private static ServiceLocator instance = new ServiceLocator();
public static ServiceLocator getInstance() {
return instance;
}
private ServiceLocator() throws ServiceLocatorException {
try {
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,
"org.jnp.interfaces.NamingContextFactory");
env.put(Context.PROVIDER_URL, "jnp://localhost:1099");
env.put("java.naming.factory.url.pkgs",
"org.jboss.naming:org.jnp.interfaces");
initialContext = new InitialContext(env);
cache = new HashMap<String, Object>();
} catch (Exception e) {
throw new ServiceLocatorException(e.toString());
}
}
public Object getRemoteInterface(String jndiName)
throws ServiceLocatorException {
Object remoteInterface = cache.get(jndiName);
if (remoteInterface == null) {
try {
remoteInterface = initialContext.lookup(jndiName);
cache.put(jndiName, remoteInterface);
} catch (Exception e) {
throw new ServiceLocatorException(e.toString());
}
}
return remoteInterface;
}
} |
enfin, le client console :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
|
package launch;
import access_jpa.CatalogDelegate;
import entities.Category;
import entities.Item;
public class first_console_1 {
/**
* @param args
*/
public static void main(String[] args) {
try {
Category cat_test = new Category();
cat_test.setDescription("description perso");
cat_test.setName("my name");
cat_test.setId(0L);
cat_test = CatalogDelegate.createCategory(cat_test);
for (Category a_category : CatalogDelegate.findCategories()) {
System.out.println(a_category);
}
} catch (Exception e) {
System.out.println(e);
}
System.out.println("c'est tout!");
}
} |
voilà, à votre bon coeur!:aie:
ah, j'oubliais : la sortie :
Code:
1 2 3 4 5
|
log4j:WARN No appenders could be found for logger (org.jnp.interfaces.TimedSocketFactory).
log4j:WARN Please initialize the log4j system properly.
java.lang.reflect.UndeclaredThrowableException
c'est tout! |