Javax Persistence unable to compile on maven - java

I have the following Java code which errors on when I run
mvn clean install
In my source code, I have the following imports
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
and in my pom.xml I had the following
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
<version>1.0.0.Final</version>
</dependency>
and I was getting the following error
error: package javax.persistence does not exist
I thought that maybe there was an error with the entry in the pom, so I tried to change the pom to the following
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>javax.persistence-api</artifactId>
<version>2.2</version>
</dependency>
and I still get the following error. I am not sure what's causing the issue and how to fix it.

Try to replace it with
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<version>1.0</version>
</dependency>

Related

ItextPDF dependency not found by Maven

I have the following dependency declared in my pom.xml file
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext7-core</artifactId>
<version>7.2.0</version>
</dependency>
I'm working in Intellij and it says it's not found. It also won't let me import anything from the com.itextpdf packages.
I've tried a couple clean builds and restarts with no change. I also tried adding the itextpdf repository to the POM file with no change.
it is
<!-- https://mvnrepository.com/artifact/com.itextpdf/itext7-core -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext7-core</artifactId>
<version>7.2.0</version>
<type>pom</type>
</dependency>

The import org.elasticsearch.transport.client cannot be resolved

I'm trying to use Elasticsearch in Java. I hava Elasticserach version 7.0.1 installed.
The following line:
import org.elasticsearch.transport.client.*;
produces the compilation error:
The import org.elasticsearch.transport.client cannot be resolved
Even though I can see that this is the correct path in the source code.
pom.xml:
<project xmlns="...">
...
<dependencies>
...
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>7.0.1</version>
</dependency>
</dependencies>
</prpject>
You have to use the below dependency for transport client.
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>7.0.1</version>
</dependency>
Looking at the source code , That package has just one class. It will be conventional to use the specific path instead of calling all.
You may want to twitch your import to
import org.elasticsearch.transport.client.PreBuiltTransportClient;
Your dependency in the pom.xml:
<project xmlns="...">
...
<dependencies>
...
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>7.0.1</version>
</dependency>
</dependencies>
</project>
That should resolve it.
Always refer to your build repository online to be sure you are correctly defining the latest version of your dependency.

Okhttp3 com.square does not exist

I am using Netbeans and I've created java project with Maven. I added this dependency.
<dependency>
<groupId>com.squareup.okhttp</groupId>
<artifactId>okhttp</artifactId>
<version>2.7.2</version>
</dependency>
It worked, I could import com.squareup.okhttp.*. After seeing some code on the web I realized that many people are using the version 3+. I tried to change the package to:
updated
I've typed groupid wrongly in the question "com.squareup.okhttp" but in my code it was right "com.squareup.okhttp3".
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.9.0</version>
But I couldn't import com.squareup.okhttp3 ( package com.squareup does not exist). Why? I am new to the Java language itself and all the IDEs and tools that support it.
The import is just okhttp3: "import okhttp3" without com.square.
The correct coordinates for okhttp3 are:
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.9.1</version>
</dependency>
Note that the groupId has changed from com.squareup.okhttp to com.squareup.okhttp3.
So, if you update your pom.xml, replacing what you had for okhttp with what I posted above then you'll be able to resolve the okhttp classes.
For future reference you can find the okhttp artifacts on Maven Central.
Okhttp3 does not exist is due to a bug in Intellij IDE; however, there is a get-around:
Place your 'com.squareup.okhttp3' dependency block at the end of the 'dependencies' list in the pom.xml file.
'import okhttp3...' is the right thing to to in your Java file.
pom.xml: 
...
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.5.0</version>
</dependency>
</dependencies>
Java file:
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

The import org.openqa.selenium.remote.CapabilityType cannot be resolved

I can't do the following two imports in Selenium 3.4.0
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
My maven dependencies for selenium is as below:
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.4.0</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-server</artifactId>
<version>3.4.0</version>
</dependency>
I'm getting error messageslike The import org.openqa.selenium.remote.CapabilityType cannot be resolved in Eclipse
What could be the problem?
As you are trying to include the following imports:
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
You have already added the org.seleniumhq.selenium and org.seleniumhq.selenium dependencies in the pom.xml.
Now, as per the Selenium 3.4.0 Documentation here as you want to use the RemoteWebDriver implementation, you still need to download the selenium-server-standalone.jar from the Selenium Download page and and then either put it into your resources folder and manipulate it through Runtime or put it somewhere else and manipulate it via command line.
Add this additional dependency in your pom.xml
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-remote-driver -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-remote-driver</artifactId>
<version>2.44.0</version>
</dependency>

Compilation failure when using #Transactional(value = "primary")

In pom.xml I use spring-tx-4.1.4.RELEASE.jar, but maven compiles the project with an error:
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.3.2:compile (default-compile) on project transaction: Compilation failure
\p4_projects\transaction\src\main\java\com\sick\dao\hibernate\DeviceModelDaoHibernate.java:[25,15] cannot find symbol symbol : method value()
location: #interface org.springframework.transaction.annotation.Transactional
I cannot find the reason. Dependency Hierarchy shows correct version 4.1.4 for all spring dependencies. I have the same result with 4.1.3.
I'll appreciate your help.
Thanks,
Elena
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import com.sick.dao.DeviceModelDao;
import com.sick.model.DeviceModel;
#Repository
#Transactional(value = "primary")
public class DeviceModelDaoHibernate implements DeviceModelDao {
private SessionFactory sessionFactory;
public DeviceModelDaoHibernate() {
}
public DeviceModelDaoHibernate(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
#Override
public void save(DeviceModel deviceModel) {
sessionFactory.getCurrentSession().saveOrUpdate(deviceModel);
}
}
pom.xml is too big to post here, therefore I publish only versions of dependencies:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<red5-server.version>1.0.2-SNAPSHOT</red5-server.version>
<red5-client.version>1.0.2-SNAPSHOT</red5-client.version>
<camel.version>2.14.1</camel.version>
<hibernate.version>4.3.8.Final</hibernate.version>
<spring.version>4.1.4.RELEASE</spring.version>
<spring-security.version>3.2.5.RELEASE</spring-security.version>
<spring-integration.version>3.0.0.RELEASE</spring-integration.version>
<slf4j.version>1.7.5</slf4j.version>
<mina.version>2.0.7</mina.version>
<logback.version>1.0.13</logback.version>
<junit.version>4.10</junit.version>
<cargo.host>localhost</cargo.host>
<cargo.port>25888</cargo.port>
<cargo.wait>false</cargo.wait>
<tomcat.version>6.0.14</tomcat.version>
</properties>
....
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<type>jar</type>
</dependency>
</dependencies>
Using Eclipse?
Try Maven clean > Project clean > Maven install.
Error is suspicious indeed, I think you do not have problem with code but with Maven.
Thanks to everybody, I have already resolved that issue. In pom.xml I've placed all org.springframework dependencies before org.hibernate dependencies and the problem has gone.
Same problem here: the #Transactional was wrongly coming from an old "spring-dao" library, wich was a transitive dependency for "spring-hibernate3" but I wanted it from "spring-tx".
Solved it by moving the "spring-tx" dependency before the "spring-hibernate3" (I supposed I could have used exclusions too).
Use mvn dependency:tree to visualize transitive dependencies.

Categories