I’ve been following the e-commerce tutorial located here: http://netbeans.org/kb/docs/javaee/ecommerce/intro.html
Code repo of project here.
I have ran into a few problems that I believe are related:
1: Trying to view the customers’ orders on the Admin page results in:
**WARNING**: EJB5184:A system exception occurred during an invocation on EJB OrderManager, method: public java.util.Map session.OrderManager.getOrderDetails(int)
**WARNING**: javax.ejb.EJBTransactionRolledbackException
**WARNING**: EJB5184:A system exception occurred during an invocation on EJB OrderedproductFacade, method: public java.util.List session.OrderedproductFacade.findByOrderId(java.lang.Object)
**WARNING**: javax.ejb.TransactionRolledbackLocalException: Exception thrown from bean
Caused by: java.lang.IllegalArgumentException: You have attempted to set a parameter value using a name of customerOrderId that does not exist in the query string SELECT o FROM Orderedproduct o WHERE o.orderedproductPK.custOrderid = :custOrderid.
2: Trying to view details for a particular order in the admin page results in:
WARNING: StandardWrapperValve[AdminServlet]: PWC1406: Servlet.service() for servlet AdminServlet threw exception
Caused by: java.lang.IllegalArgumentException: You have attempted to set a parameter value using a name of customerOrderId that does not exist in the query string SELECT o FROM Orderedproduct o WHERE o.orderedproductPK.custOrderid = :custOrderid.
Both problems have the ‘findByOrderId’ method in common and I am at a loss as to what is wrong with it.
The offending method is located in the following directory: src/jsf_crud/src/java/session/OrderedProductFacade.java
(I would link it as a hyperlink but spam prevention measures prevent me)
Not sure what the best course of action is, any recommendations?
Your query needs a parameter called "custOrderid" and not "customerOrderId"
Either change the query or change the called parameter.
The query in the OrderedProduct class uses "customerOrderId"
http://netbeans.org/projects/samples/sources/samples-source-code/content/samples/javaee/AffableBean/src/java/entity/OrderedProduct.java
Related
I have a query like:
#Query("select I from Indicator I where I.bankId in (:bankIds) ")
public List<Indicator> getIndicatorDetailsByBankIdList(#Param("bankIds") Set<Long> bankIdList);
This is working fine most of the time. At times it's throwing the following exception.
Description - Parameter with that name [bankIds] did not exist; nested exception is java.lang.IllegalArgumentException: Parameter with that name [bankIds] did not exist, StackTrace - java.lang.IllegalArgumentException: Parameter with that name [bankIds] did not exist
at org.hibernate.jpa.spi.BaseQueryImpl.findParameterRegistration(BaseQueryImpl.java:505)
at org.hibernate.jpa.spi.BaseQueryImpl.setParameter(BaseQueryImpl.java:631)
at org.hibernate.jpa.spi.AbstractQueryImpl.setParameter(AbstractQueryImpl.java:180)
at org.hibernate.jpa.spi.AbstractQueryImpl.setParameter(AbstractQueryImpl.java:49)
Observations
I'm not sure how right i'm adding in this observation, but i guess the service fails only when used inside modules which are little on the data heavy side (even here it fails only at times). Does memory issues cause these kind of exceptions to be thrown?
Inconsistant issue was observed on another method on the same entity:
#Query("select I from Indicator I where I.activityId in (:activityIds) ")
public List<Indicator> getIndicatorDetailsByActivityIdList(#Param("activityIds") Set<Long> acyIdList);
I have JSF page that is trying to call a method in my managed bean, but is throwing an error that I am calling an ambiguous method:
<div class="Container100 MarTop5 #{surveyBean.getBackGroundStyleClass(cc.attrs.multiChoiceItem.answer)}">
The error is:
SEVERE: /resources/component/checkMany2.xhtml: Unable to find unambiguous method: class org.beans.questionnaire.SurveyBean.getBackGroundStyleClass(null)
javax.el.ELException: /resources/component/checkMany2.xhtml: Unable to find unambiguous method: class org.beans.questionnaire.SurveyBean.getBackGroundStyleClass(null)
The problem is that my SurveyBean class has overloaded getBackGroundStyleClass() methods. Consequently, when the cc.attrs.multiChoiceItem.answer is null, it does not know which method to call.
In standard Java, I would be able to cast the answer to the type I want to ensure that Java can find the correct method.
How can I do this in EL? Is it feasible? I've tried:
but that failed miserably:
Failed to parse the expression [#{surveyBean.getBackGroundStyleClass((Answer)cc.attrs.multiChoiceItem.answer)}]
I also tried:
#{surveyBean.getBackGroundStyleClass(Answer.class.cast(cc.attrs.multiChoiceItem.answer))}
but that too failed with:
javax.el.ELException: The identifier [class] is not a valid Java identifier as required by section 1.19 of the EL specification (Identifier ::= Java language identifier). This check can be disabled by setting the system property org.apache.el.parser.SKIP_IDENTIFIER_CHECK to true.
Is there some way in EL to cast the argument type?
I have encountered a very strange exception, and I don't know how to find the reason.
Business background:
Add goods and meantime it's price list, a goods have 5 price for diff level user.
In controller, first convert goodForm to goods by using dozer, then call goodsService to save goods.
In goodsService after saving goods, traversal goods price list and populate goodsId to goods price,
GoodsForm:
#Mapping("priceList")
List<GoodsPriceForm> goodsPriceFormList;
Goods:
List<GoodsPrice> priceList;
Controller:
Goods goods = BeanMapper.map(goodsForm, Goods.class);
goodsService.saveGoods(adminId, goods);
GoodsService:
goodsDao.save(goods);
goods.getPriceList().forEach(p -> p.setGoodsId(goods.getId()));
goodsPriceDao.save(goods.getPriceList());
But it throw exception:
2015-11-27 17:10:57,042 [http-nio-8081-exec-8] ERROR o.a.catalina.core.ContainerBase.[Tomcat].[localhost].[/].[dispatcherServlet] - Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.ClassCastException: com.foo.goods.model.GoodsPrice cannot be cast to com.foo.goods.model.GoodsPrice] with root cause
java.lang.ClassCastException: com.foo.goods.model.GoodsPrice cannot be cast to com.foo.goods.model.GoodsPrice
at com.foo.goods.service.GoodsService$$Lambda$11/310447431.accept(Unknown Source) ~[na:na]
at java.util.ArrayList.forEach(ArrayList.java:1249) ~[na:1.8.0_51]
at com.foo.goods.service.GoodsService.saveGoods(GoodsService.java:34) ~[classes/:na]
This error message let me feel very confused. In addition I write a unit test wanted to repeat this, but failed.
GoodsForm form = new GoodsForm();
form.setGoodsPriceFormList(Lists.newArrayList(new GoodsPriceForm((byte) 1, BigDecimal.valueOf(10)),
new GoodsPriceForm((byte) 2, BigDecimal.valueOf(9)),
new GoodsPriceForm((byte) 3, BigDecimal.valueOf(8))));
Goods goods = BeanMapper.map(form, Goods.class);
goods.getPriceList().forEach(p -> p.setGoodsId(goods.getId()));
Run this unit test, it executed ok.
So why in real web situation(Spring boot + Jpa) it's failed, but in unit test situation it's ok?
Controller:
System.out.println("PriceList: " + goods.getPriceList().getClass().getClassLoader());//PriceList: null
System.out.println(goods.getPriceList().get(0).getClass().getClassLoader()); //java.lang.ClassCastException: com.foo.goods.model.GoodsPrice cannot be cast to com.foo.goods.model.GoodsPrice
If I generated a packaged jar, then execute this jar
java -jar target/myapp.jar
In this case without above exception.
And I commented spring-boot-devtools in pom.xml, then started application, without above exception.
By default, any open project in your IDE will be loaded using the “restart” classloader, and any regular .jar file will be loaded using the “base” classloader. If you work on a multi-module project, and not each module is imported into your IDE, you may need to customize things. To do this you can create a META-INF/spring-devtools.properties file.
The spring-devtools.properties file can contain restart.exclude. and restart.include. prefixed properties. The include elements are items that should be pulled-up into the “restart” classloader, and the exclude elements are items that should be pushed down into the “base” classloader. The value of the property is a regex pattern that will be applied to the classpath.
My Solution: put META-INF/spring-devtools.properties inside resources folder, and add this content
restart.include.dozer=/dozer-5.5.1.jar
Please see : http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#using-boot-devtools-customizing-classload
You are using two different ClassLoader here. An identical Class loaded with two different ClassLoader is considered as two different Class by the JVM.
The solution to fix this is simple : Use an Interface.
Interfaces are able to abstract this problem, and you can interchange the object they implement between ClassLoaders without limitation, as long as you don't reference the implementation directly.
I have an entity that represents the root element of a large xml file that was unmarshalled from xml to java using jaxb. I am trying to persist it using hibernate EntityManager.persist(elementname), but it is throwing a HibernateException. The exception message simply states that it cannot cast a Boolean value as a specific object type.
Here is the code that is throwing the error at the line persist(cd):
public Long saveToDatabase(ClinicalDocument cd){
Long id = null;
try{
final EntityManager saveManager = entityManagerFactory.createEntityManager();
saveManager.getTransaction().begin();
saveManager.persist(cd);
saveManager.getTransaction().commit();
saveManager.close();
//After the object is saved, we can get the generated id:
id = cd.getHjid();
}catch(HibernateException sqle){sqle.printStackTrace();}
return id;
}
I uploaded a stripped down working eclipse project that can enable you to recreate this problem in a couple minutes. It is a zip file that you can download from this link. To recreate the problem on your machine, just:
1.) unzip the folder
2.) import the folder into eclipse as an existing project
3.) change `persistence.properties` to include a valid username, password, and database name on your machine
4.) then right click on `Main.java` and choose `Run As..Java Application`.
It will then recreate the error. You can then review the schema in the schema.xsd file and the stripped down xml in the po.xml file.
You can view the directory structure and see the locations of persistence.properties, po.xml, schema.xsd, and Main.java in the following screen shot below:
I also get the following stack trace:
Exception in thread "main" javax.persistence.PersistenceException: org.hibernate.HibernateException: Unable to resolve entity name from Class [java.lang.Boolean] expected instance/subclass of [org.jvnet.hyperjaxb3.ejb.tests.pocustomized.II]
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1215)
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1148)
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1154)
at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:678)
at productionmain.DataFunctions.saveToDatabase(DataFunctions.java:265)
at productionmain.Main.main(Main.java:21)
Caused by: org.hibernate.HibernateException: Unable to resolve entity name from Class [java.lang.Boolean] expected instance/subclass of [org.jvnet.hyperjaxb3.ejb.tests.pocustomized.II]
at org.hibernate.tuple.entity.PojoEntityTuplizer.determineConcreteSubclassEntityName(PojoEntityTuplizer.java:360)
at org.hibernate.persister.entity.AbstractEntityPersister.getSubclassEntityPersister(AbstractEntityPersister.java:3941)
at org.hibernate.impl.SessionImpl.getEntityPersister(SessionImpl.java:1494)
at org.hibernate.engine.ForeignKeys.isTransient(ForeignKeys.java:202)
at org.hibernate.event.def.AbstractSaveEventListener.getEntityState(AbstractSaveEventListener.java:531)
at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:102)
at org.hibernate.impl.SessionImpl.firePersist(SessionImpl.java:799)
at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:791)
at org.hibernate.engine.EJB3CascadingAction$1.cascade(EJB3CascadingAction.java:48)
at org.hibernate.engine.Cascade.cascadeToOne(Cascade.java:392)
at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:335)
at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:204)
at org.hibernate.engine.Cascade.cascade(Cascade.java:161)
at org.hibernate.event.def.AbstractSaveEventListener.cascadeBeforeSave(AbstractSaveEventListener.java:450)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:282)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:203)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:129)
at org.hibernate.ejb.event.EJB3PersistEventListener.saveWithGeneratedId(EJB3PersistEventListener.java:69)
at org.hibernate.event.def.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:179)
at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:135)
at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:61)
at org.hibernate.impl.SessionImpl.firePersist(SessionImpl.java:808)
at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:782)
at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:786)
at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:672)
... 2 more
I believe you have a weird naming colission here. You have two properties named id and setId. Per property, JAXB generated two a getter (getXXX) and, if instructed "isSetter" to check if the property is set - useful for primitive types. This "isSetter" is named isSetXXX.
In this case your isSetter for setId collides with the getter for id.
Issetter for setId:
#Transient
public boolean isSetId() {
return (this.id!= null);
}
Getter for id:
public II getSetId() {
return setId;
}
Note isSetId vs. getSetId. This probably makes Hibernate crazy because it thinks that isSetId is a boolean getter whereas it expects an enity getter.
Solution: rename the setId property in you model using jaxb:property in your bindings file.
I'm getting the following error in ours logs:
Error looking up property "foo" in
object type "foo.bar". Cause: null
java.lang.reflect.InvocationTargetException
at sun.reflect.GeneratedMethodAccessor363.invoke(Unknown
Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.commons.beanutils.PropertyUtilsBean.invokeMethod(PropertyUtilsBean.java:1773)
I cannot for the life of me recreate it, I was wondering if anyone has any experience with this kind of problem with JSP/Java Bean. What I wanted to know was, will this prevent the user from getting the web page to show up?
I know this isn't a whole lot of information, but any advice could help.
Something on some page is trying to "navigate" into a bean instance (a Java object, that is), and it's trying to get to a property that isn't there on the bean in question.
<span id='name'>${fn:escapeXml(someBean.user.fullName)}</span>
If the bean "someBean" has no "user" property, of if the user object has no "fullName" property, you get an exception like that.
From what you're giving here, the only suggestion I have is to make sure that you have indeed a property called "foo" and to not have a period in "foo.bar". You cannot name your variables/objects using a period in the name. JSP will automatically go and look for a property called "bar" in "foo". Call it instead "fooBar".
Java is calling the getter method on the bean providing the property which is in turn throwing an exception. If you can see the target exception - that is the target of InvocationTargetException you will know what is causing this to fail.