vrijdag 12 augustus 2011

getClass().getResource(filename) doesn't work if file is still in jar

I was moving an application from Tomcat to Weblogic and by default in Tomcat the jar/war is extracted whereas in Weblogic this isn't at least not in our setup.

So my code to read a file within my applications classpath was not longer working.

initial code:
URL input = getClass().getResource(queryFileName);
File file = new File(input.getFile());

However the file is not null it's not readable and throws a FileNotFoundException

new code:
InputStream stream;
stream = getClass().getResourceAsStream(queryFileName);

The only disadvantage is you have an InputStream instead of a file.


maandag 25 juli 2011

ERROR - java.lang.IllegalArgumentException: Illegal attempt to dereference path source [null]

Using: JPA 2 with hibernate


Stacktrace:
java.lang.IllegalArgumentException: Illegal attempt to dereference path source [null]
at org.hibernate.ejb.criteria.path.AbstractPathImpl.illegalDereference(AbstractPathImpl.java:104)
at org.hibernate.ejb.criteria.path.AbstractPathImpl.get(AbstractPathImpl.java:186)

Cause:
I was doing a get on a PluralAttributePath

initial code:
Root root = criteriaQuery.from(Order.class);
Path path = root.get(key); //path to OrderLines
path = path.get("description"); //throws exception since Ordelines was a many to many join

new code:
Root root = criteriaQuery.from(Order.class);
Join join = criteriaQuery.join("orderLines");
Path path = root.get(key); //path to OrderLines
path = join.get("description");