I want to have a pmd rule for my java project which will disallow field injections in Spring(want to use injection by constructor instead). is there any pmd rule for it, or can I create some to have it in my code analysis?
To not allow this:
#Autorired
private Object object;
thanks!
You could create an XPath rule with the following expression:
//Annotation
[pmd-java:typeIs('org.springframework.beans.factory.annotation.Autowired')]
[../FieldDeclaration]
To add this to your ruleset XML, see here:
<rule name="todo"
language="java"
message="todo."
class="net.sourceforge.pmd.lang.rule.XPathRule" >
<description>
TODO
</description>
<priority>3</priority>
<properties>
<property name="xpath">
<value>
<![CDATA[
//Annotation
[pmd-java:typeIs('org.springframework.beans.factory.annotation.Autowired')]
[../FieldDeclaration]
]]>
</value>
</property>
</properties>
</rule>
Is there any PMD or Checkstyle rule available that could help me to prohibit usage of some certain classes in Java code?
In my case I'd like to ban all of the following in all possible contexts:
org.apache.commons.lang3.CharEncoding
org.apache.commons.lang.CharEncoding
org.apache.commons.codec.CharEncoding
I've found IllegalImport check, but it's about packages, not particular classes.
For PMD you can write rule like this:
<rule name="Prohibited classes"
language="java"
message="Avoid using these classes."
class="net.sourceforge.pmd.lang.rule.XPathRule" >
<description>
Avoid using these classes, there are better alternatives.
</description>
<priority>3</priority>
<properties>
<property name="xpath">
<value>
<![CDATA[
//Name[pmd-java:typeIs('org.apache.commons.lang3.CharEncoding')] |
//Name[pmd-java:typeIs('org.apache.commons.lang.CharEncoding')] |
//Name[pmd-java:typeIs('org.apache.commons.codec.CharEncoding')]
]]>
</value>
</property>
</properties>
</rule>
Or //Name[starts-with(#Image, 'com.sun.')] to prohibit package import.
You can try to use these pmd extensions to blacklist classes and methods: https://github.com/LiveRamp/pmd_extensions
I keep getting an error saying:
cannot resolve org.apache.openjpa.persistence.PersistenceProviderImpl
My jar file is there but I am having a hard time trying to figure out why I am still getting the error:
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
<persistence-unit name="dataBase">
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<properties>
<property name="openjpa.ConnectionURL" value="com.mysql.jdbc.Driver"/>
<property name="openjpa.ConnectionDriverName" value="jdbc:mysql://localhost:8080/springpractice"/>
<property name="openjpa.ConnectionUserName" value="root"/>
<!--<property name="openjpa.ConnectionPassword" value=""/>-->
<property name="openjpa.Log" value="DefaultLevel=TRACE, Tool=INFO"/>
</properties>
</persistence-unit>
</persistence>
I have look all over google and the answer is not there. I double check to make sure the dependencies are not there twice in case there was a conflict. Because of this when I try to declare a #Entity it is not picking up the database tables.
Based on the images of your lib directory, I would say you the XML definition for persistence-unit>provider is not as expected.
There are Hibernate Jars in your lib, so it seem like that
"Persistence Provider" should be "Hibernate", and not "Apache OpenJPA".
If you really want to use Apache OpenJPA, then I would recommend removing Hibernate Jar files in your lib.
Hibernate is more popular of the two, so I would recommend it.
You have to import a JPA implementation jar (OpenJPA in this case). You could get one from Maven Central or Apache OpenJPA home page itself.
UPDATED: as it has been mentioned in another ansewer it's better to remove the other JPA provider libraries (Hibernate).
I had to add the following jar:
org.apache.openjpa:com.springsource.org.apache.openjpa.persistence:1.02
I trusted my IDE to much that it would download all the jars that I needed.
I would like to make checkstyle ignore missing javadoc for #throws clauses in tests, but complain in non-tests.
Because supression files do not support subproperties of the JavadocMethod, I am forced to either ignore javadoc in tests altogether or not at all, with no granularity. This is also the conclusion of this other question.
Here's what I'm trying now: could I have two JavadocMethod modules with different id's (e.g. test and notest) in my checkstyle configuration, and selectively supress the test module on non-tests and viceversa?
I cannot get this to work, and I'm starting to think I can't have a duplicate module in checkstyle. Any insights?
Here are my rules regarding JavadocMethod in the configuration xml:
<module name="JavadocMethod">
<property name="id" value="nontest"/>
</module>
<module name="JavadocMethod">
<property name="id" value="test"/>
<property name="allowMissingThrowsTags" value="true"/>
</module>
And this is my supression file:
<suppressions>
<!-- Supress non-test-doc on tests -->
<suppress id="nontestdoc" files=".*(?:Test|IT).*java" />
<suppress id="testdoc" files=".*(?!Test|IT).*java" />
</suppressions>
Thanks!
You can have duplicate module in checkstyle.
Tried with 2 Indentation module with tab 2 and tab 4 respectively. And used the suppression.xml like following
<?xml version="1.0"?>
<!DOCTYPE suppressions PUBLIC
"-//Puppy Crawl//DTD Suppressions 1.1//EN"
"http://www.puppycrawl.com/dtds/suppressions_1_1.dtd">
<suppressions>
<suppress id="IndnA" files=".*\.java"/>
<suppress id="IndnB" files=".*\.java"/>
</suppressions>
and what is the regex .*(?:Test|IT).*java and .*(?!Test|IT).*java for? Why are you using LookAhead?
please check with files as .*\.java whether that is working or not.
I am new to JPA & Hibernate. After reading some online materials I now understand what Hibernate is and how it can be used with JPA.
Now, I am trying to run this JPA & Hibernate tutorial. I've done everything they mention in this tutorial.
I don't have Oracle DB, only MySQL. So I made some changes to persistence.xml using my understanding of JPA & Hibernate (I don't know if it's correct or not... Seems to me it is.)
Here is my persistence.xml
<persistence 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_1_0.xsd" version="1.0">
<persistence-unit name="customerManager" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>Customer</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLInnoDBDialect"/>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.connection.username" value="root"/>
<property name="hibernate.connection.password" value="1234"/>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/general"/>
<property name="hibernate.max_fetch_depth" value="3"/>
</properties>
</persistence-unit>
</persistence>
But I don't seem to get the output they describe. It's giving me:
Customer id before creation:null
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.annotations.Version).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named customerManager
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:55)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:33)
at CustomerDAO.create(CustomerDAO.java:8)
at CustomerDAO.main(CustomerDAO.java:22)
Any suggestions will be appreciated.
Update:
I have made the changes that are asked to done. But, still getting the asme error lines!!!
They didnt mentioned anything about orm.xml in that tutorial. may it be a problem causer!!!
Just for completeness. There is another situation causing this error:
missing META-INF/services/javax.persistence.spi.PersistenceProvider
file.
For Hibernate, it's located in hibernate-entitymanager-XXX.jar, so, if hibernate-entitymanager-XXX.jar is not in your classpath, you will got this error too.
This error message is so misleading, and it costs me hours to get it correct.
See JPA 2.0 using Hibernate as provider - Exception: No Persistence provider for EntityManager.
Your persistence.xml is not valid and the EntityManagerFactory can't get created. It should be:
<persistence 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_1_0.xsd" version="1.0">
<persistence-unit name="customerManager" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>Customer</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLInnoDBDialect"/>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.connection.username" value="root"/>
<property name="hibernate.connection.password" value="1234"/>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/general"/>
<property name="hibernate.max_fetch_depth" value="3"/>
</properties>
</persistence-unit>
</persistence>
(Note how the <property> elements are closed, they shouldn't be nested)
Update: I went through the tutorial and you will also have to change the Id generation strategy when using MySQL (as MySQL doesn't support sequences). I suggest using the AUTO strategy (defaults to IDENTITY with MySQL). To do so, remove the SequenceGenerator annotation and change the code like this:
#Entity
#Table(name="TAB_CUSTOMER")
public class Customer implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
#Column(name="CUSTOMER_ID", precision=0)
private Long customerId = null;
...
}
This should help.
PS: you should also provide a log4j.properties as suggested.
I had the same problem today. My persistence.xml was in the wrong location. I had to put it in the following path:
project/src/main/resources/META-INF/persistence.xml
I was facing the same issue. I realised that I was using the Wrong provider class in persistence.xml
For Hibernate it should be
<provider>org.hibernate.ejb.HibernatePersistence</provider>
And for EclipseLink it should be
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
If you use Hibernate 5.2.10.Final, you should change
<provider>org.hibernate.ejb.HibernatePersistence</provider>
to
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
in your persistence.xml
According to Hibernate 5.2.2: No Persistence provider for EntityManager
If you are using Maven you may have both src/{main,test}/resources/META-INF/persistence.xml. This is a common setup: test your JPA code with h2 or Derby and deploy it with PostgreSQL or some other full DBMS. If you're using this pattern, do make sure the two files have different unit names, else some versions of the Persistence class will try to load BOTH (because of course your test-time CLASSPATH includes both classes and test-classes); this will cause conflicting definitions of the persistence unit, resulting in the dreaded annoying message that we all hate so much!
Worse: this may "work" with some older versions of e.g., Hibernate, but fail with current versions. Worth getting it right anyway...
A bit too late but I got the same issue and fixed it switching schemalocation into schemaLocation in the persistence.xml file (line 1).
I have seen this error , for me the issue was there was a space in the absolute path of the persistance.xml , removal of the same helped me.
I was also facing the same issue when I was trying to get JPA entity manager configured in Tomcat 8. First I has an issue with the SystemException class not being found and hence the entityManagerFactory was not being created. I removed the hibernate entity manager dependency and then my entityManagerFactory was not able to lookup for the persistence provider. After going thru a lot of research and time got to know that hibernate entity manager is must to lookup for some configuration. Then put back the entity manager jar and then added JTA Api as a dependency and it worked fine.
my experience tells me that missing persistence.xml,will generate the same exception too.
i caught the same error msg today when i tried to run a jar package packed by ant.
when i used jar tvf to check the content of the jar file, i realized that "ant" forgot to pack the persistnece.xml for me.
after I manually repacked the jar file ,the error msg disappered.
so i believe maybe you should try simplely putting META-INF under src directory and placing your persistence.xml there.