JPQL doesn't support UPDATE/DELETE?


#1

Here
https://docs.jboss.org/hibernate/stable/ogm/reference/en-US/html_single/#ogm-jpql-query
is stated JPQL doesn’t currently support update/delete queris?
So basically JPQL cannot be used for complete CRUD operations on an entity?


#2

So basically JPQL cannot be used for complete CRUD operations on an entity?

I guess you are correct.
If you need to run an update/delete query you have to use native queries at the moment.


#3

wait…maybe I’m missing something.
How can I use at all Hibernate OGM with JPQL in production if I cannot operate CRUD?
The persist() operation on EntityManager includes implicitly an UPDATE too, am I right?


#4

A JPQL query is something like:

em.createQuery( "UPDATE Helicopter SET name = 'prova' WHERE name = :name" )
				.setParameter( "name", POLICE_HELICOPTER )
				.executeUpdate();

This queries are not supported unless you are doing a SELECT.

But you can update entities using the EntityManager or Session via the persist,save, delete and so on methods.
If you are dealing with a managed entity you can simply update the object and commit the transaction.

For example:

// Persist
Transaction transaction = session.beginTransaction();
Hypothesis hyp = new Hypothesis();
hyp.setId( "1234567890" );
hyp.setDescription( "NP != P" );
session.persist( hyp );
transaction.commit();

// Update
Transaction transaction = session.beginTransaction();
Hypothesis h = (Hypothesis) session.get( Hypothesis.class, "1234567890" );
h.setDescription( "New Hypothesis description" );
transaction.commit();

// Delete
Transaction transaction = session.beginTransaction();
Hypothesis h = (Hypothesis) session.get( Hypothesis.class,  "1234567890" );
session.delete( h );
transaction.commit();

If you want some more details, myou have to tell me what’s confusing about it because your question is not very clear.


#5

In the example I used the session but it works in a similar way with the EntityManager. You can use find instead of get and remove instead of delete.


#6

Quick related question:

EntityManagerFactory emfactory = Persistence.createEntityManagerFactory("MongoEM");
EntityManager entitymanager = emfactory2.createEntityManager();
Query query = e.createQuery("FROM PolyEventBody e WHERE e.id=:id");
query.setParameter("id",-9157766204783718999L);
PolyEventBody r = (PolyEventBody) query.getSingleResult();
entitymanager.remove(r);

why doesn’t this work?
obviously the query returns an entity.


#7

It would be nice if you could be more specific when you ask questions.

Why doesn’t this work?

What kind of problem do you experience? Is there an exception? Do you mean the entity is not removed?

My guess is that you haven’t used transactions nor flushed the operations: https://docs.jboss.org/hibernate/stable/ogm/reference/en-US/html_single/#_on_flush_and_transactions

EDIT: Removed the line “Or maybe it doesn’t find it?” (You actually mentioned that it can find it)


#8

You’re right, I wrote the post in hurry and it was very inaccurate. And you were right, it was about the missing transaction. Thanks :+1:


#9

still related question: how about native queries? Does hibernate have a full support to MongoDB syntax?

I tried this:

javax.transaction.TransactionManager tx = com.arjuna.ats.jta.TransactionManager.transactionManager();
EntityManager mem = mdm.getEntityManager();
tx.begin();
mem.createNativeQuery("db.MyCollection.remove({})").executeUpdate();
tx.commit();

I get this error:
Exception in thread "main" java.lang.IncompatibleClassChangeError: class org.objectweb.asm.tree.ClassNode has interface org.objectweb.asm.ClassVisitor as super class


#10

This is the pom, if it can be helpful

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>it.mygroup.dev</groupId>
	<artifactId>myartifact</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>
	<name>myartifact</name>
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<jmh.version>1.10.3</jmh.version>
		<javac.target>1.7</javac.target>
	</properties>
		<dependencies>
		<!-- https://mvnrepository.com/artifact/org.eclipse.persistence/eclipselink -->
		<dependency>
			<groupId>org.eclipse.persistence</groupId>
			<artifactId>eclipselink</artifactId>
			<version>2.5.2</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.eclipse.persistence/javax.persistence -->
		<dependency>
			<groupId>org.eclipse.persistence</groupId>
			<artifactId>javax.persistence</artifactId>
			<version>2.1.0</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>3.0.1</version>
			<scope>provided</scope>
		</dependency>

		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.11</version>
		</dependency>
		<dependency>
			<groupId>asm</groupId>
			<artifactId>asm</artifactId>
			<version>3.3.1</version>
		</dependency>
		<dependency>
			<groupId>commons-beanutils</groupId>
			<artifactId>commons-beanutils</artifactId>
			<version>1.8.0</version>
		</dependency>
		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-csv</artifactId>
			<version>1.4</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
		<dependency>
			<groupId>commons-fileupload</groupId>
			<artifactId>commons-fileupload</artifactId>
			<version>1.2</version>
		</dependency>

		<dependency>
			<groupId>commons-io</groupId>
			<artifactId>commons-io</artifactId>
			<version>1.4</version>
		</dependency>
		<dependency>
			<groupId>commons-lang</groupId>
			<artifactId>commons-lang</artifactId>
			<version>2.4</version>
		</dependency>
		<dependency>
			<groupId>commons-logging</groupId>
			<artifactId>commons-logging</artifactId>
			<version>1.0.4</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/dom4j/dom4j -->
		<dependency>
			<groupId>dom4j</groupId>
			<artifactId>dom4j</artifactId>
			<version>1.6.1</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.freemarker/freemarker -->
		<dependency>
			<groupId>org.freemarker</groupId>
			<artifactId>freemarker</artifactId>
			<version>2.3.23</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.codehaus.jackson/jackson-core-asl -->
		<dependency>
			<groupId>org.codehaus.jackson</groupId>
			<artifactId>jackson-core-asl</artifactId>
			<version>1.9.2</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.codehaus.jackson/jackson-jaxrs -->
		<dependency>
			<groupId>org.codehaus.jackson</groupId>
			<artifactId>jackson-jaxrs</artifactId>
			<version>1.9.2</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.codehaus.jackson/jackson-mapper-asl -->
		<dependency>
			<groupId>org.codehaus.jackson</groupId>
			<artifactId>jackson-mapper-asl</artifactId>
			<version>1.9.2</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.codehaus.jackson/jackson-xc -->
		<dependency>
			<groupId>org.codehaus.jackson</groupId>
			<artifactId>jackson-xc</artifactId>
			<version>1.9.2</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/com.sun.jersey/jersey-client -->
		<dependency>
			<groupId>com.sun.jersey</groupId>
			<artifactId>jersey-client</artifactId>
			<version>1.19.1</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/com.sun.jersey/jersey-core -->
		<dependency>
			<groupId>com.sun.jersey</groupId>
			<artifactId>jersey-core</artifactId>
			<version>1.19.1</version>
		</dependency>

		<!-- https://mvnrepository.com/artifact/com.sun.jersey/jersey-json -->
		<dependency>
			<groupId>com.sun.jersey</groupId>
			<artifactId>jersey-json</artifactId>
			<version>1.19.1</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/com.sun.jersey.contribs/jersey-multipart -->
		<dependency>
			<groupId>com.sun.jersey.contribs</groupId>
			<artifactId>jersey-multipart</artifactId>
			<version>1.19.1</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/com.sun.jersey/jersey-server -->
		<dependency>
			<groupId>com.sun.jersey</groupId>
			<artifactId>jersey-server</artifactId>
			<version>1.19.1</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/com.sun.jersey/jersey-servlet -->
		<dependency>
			<groupId>com.sun.jersey</groupId>
			<artifactId>jersey-servlet</artifactId>
			<version>1.19.1</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.codehaus.jettison/jettison -->
		<dependency>
			<groupId>org.codehaus.jettison</groupId>
			<artifactId>jettison</artifactId>
			<version>1.1</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.bitbucket.b_c/jose4j -->
		<dependency>
			<groupId>org.bitbucket.b_c</groupId>
			<artifactId>jose4j</artifactId>
			<version>0.4.4</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.jsoup/jsoup -->
		<dependency>
			<groupId>org.jsoup</groupId>
			<artifactId>jsoup</artifactId>
			<version>1.4.1</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/javax.ws.rs/jsr311-api -->
		<dependency>
			<groupId>javax.ws.rs</groupId>
			<artifactId>jsr311-api</artifactId>
			<version>1.1.1</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/jstl/jstl -->
		<dependency>
			<groupId>jstl</groupId>
			<artifactId>jstl</artifactId>
			<version>1.2</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/log4j/log4j -->
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.13</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/com.sun.mail/javax.mail -->
		<dependency>
			<groupId>com.sun.mail</groupId>
			<artifactId>javax.mail</artifactId>
			<version>1.5.0</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.jvnet.mimepull/mimepull -->
		<dependency>
			<groupId>org.jvnet.mimepull</groupId>
			<artifactId>mimepull</artifactId>
			<version>1.9.3</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
		<dependency>
			<groupId>org.apache.poi</groupId>
			<artifactId>poi</artifactId>
			<version>3.9</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-excelant -->
		<dependency>
			<groupId>org.apache.poi</groupId>
			<artifactId>poi-excelant</artifactId>
			<version>3.9</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
		<dependency>
			<groupId>org.apache.poi</groupId>
			<artifactId>poi-ooxml</artifactId>
			<version>3.9</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml-schemas -->
		<dependency>
			<groupId>org.apache.poi</groupId>
			<artifactId>poi-ooxml-schemas</artifactId>
			<version>3.9</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-scratchpad -->
		<dependency>
			<groupId>org.apache.poi</groupId>
			<artifactId>poi-scratchpad</artifactId>
			<version>3.9</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>1.7.12</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.apache.xmlbeans/xmlbeans -->
		<dependency>
			<groupId>org.apache.xmlbeans</groupId>
			<artifactId>xmlbeans</artifactId>
			<version>2.3.0</version>
		</dependency>
		

<!-- mongodb deps -->
 
		<dependency>
			<groupId>org.hibernate.ogm</groupId>
			<artifactId>hibernate-ogm-core</artifactId>
			<version>5.1.0.Final</version>
		</dependency>
		<dependency>
			<groupId>org.hibernate.ogm</groupId>
			<artifactId>hibernate-ogm-mongodb</artifactId>
			<version>5.1.0.Final</version>
		</dependency>
		<dependency>
			<groupId>org.jboss.jbossts</groupId>
			<artifactId>jbossjta</artifactId>
			<version>4.16.6.Final</version>
		</dependency>
		<dependency>
			<groupId>javax</groupId>
			<artifactId>javaee-web-api</artifactId>
			<version>7.0</version>
			<scope>provided</scope>
		</dependency>
    <dependency>
      <groupId>javax.transaction</groupId>
      <artifactId>jta</artifactId>
      <version>1.1</version>
  </dependency>
  
<!-- microbench deps -->
		<dependency>
			<groupId>org.openjdk.jmh</groupId>
			<artifactId>jmh-core</artifactId>
			<version>${jmh.version}</version>
		</dependency>
		<dependency>
			<groupId>org.openjdk.jmh</groupId>
			<artifactId>jmh-generator-annprocess</artifactId>
			<version>${jmh.version}</version>
			<scope>provided</scope>
		</dependency>
		
	</dependencies>
	<build>
		<sourceDirectory>src</sourceDirectory>
		<resources>
			<resource>
				<directory>src</directory>
				<excludes>
					<exclude>**/*.java</exclude>
				</excludes>
			</resource>
		</resources>
		<plugins>
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.5.1</version>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-war-plugin</artifactId>
				<version>2.1.1</version>
				<configuration>
					<warSourceDirectory>WebContent</warSourceDirectory>
					
				</configuration>
			</plugin>
		</plugins>
	</build>
	
</project>

#11

In relation to the native queries, you can find information about supported queries in this paragraph: https://docs.jboss.org/hibernate/stable/ogm/reference/en-US/html_single/#ogm-mongodb-queries-native

I don’t think we cover it 100% but it’s pretty well supported. The query you mention should work. We have testcases for remove: https://github.com/hibernate/hibernate-ogm/blob/master/mongodb/src/test/java/org/hibernate/ogm/datastore/mongodb/test/query/nativequery/MongoDBSessionCLIQueryTest.java#L199

I also tried without any filter like in your example. Could you provide a test case?
That’s a weird exception, I don’t think it’s caused by the native query support. Seems more related to some incompatible version of the libraries you are using (just guessing)