Skip to content

Commit

Permalink
JAMES-2008 Solve various JPA troubbles
Browse files Browse the repository at this point in the history
  • Loading branch information
chibenwa authored and aduprat committed Apr 26, 2017
1 parent 8f0df01 commit e9e1f55
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 70 deletions.
Expand Up @@ -41,7 +41,7 @@ public static JpaTestCluster create(Class<?>... clazz) {
public static JpaTestCluster create(List<Class<?>> clazz) {
HashMap<String, String> properties = new HashMap<String, String>();
properties.put("openjpa.ConnectionDriverName", org.h2.Driver.class.getName());
properties.put("openjpa.ConnectionURL", "jdbc:h2:mem:mailboxintegration;DB_CLOSE_DELAY=-1"); // Memory H2 database
properties.put("openjpa.ConnectionURL", "jdbc:h2:mem:mailboxintegrationtesting;DB_CLOSE_DELAY=-1"); // Memory H2 database
properties.put("openjpa.jdbc.SynchronizeMappings", "buildSchema(ForeignKeys=true)"); // Create Foreign Keys
properties.put("openjpa.jdbc.MappingDefaults", "ForeignKeyDeleteAction=restrict, JoinForeignKeyDeleteAction=restrict");
properties.put("openjpa.jdbc.SchemaFactory", "native(ForeignKeys=true)");
Expand Down Expand Up @@ -84,7 +84,7 @@ public void clear(List<String> tables) {
EntityManager entityManager = entityManagerFactory.createEntityManager();
entityManager.getTransaction().begin();
for(String tableName: tables) {
entityManager.createNativeQuery("TRUNCATE table " + tableName).executeUpdate();
entityManager.createNativeQuery("DELETE FROM " + tableName).executeUpdate();
}
entityManager.getTransaction().commit();
entityManager.close();
Expand Down
26 changes: 1 addition & 25 deletions mailbox/jpa/pom.xml
Expand Up @@ -112,36 +112,12 @@
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>openjpa-maven-plugin</artifactId>
<version>1.1</version>
<version>1.2</version>
<configuration>
<includes>org/apache/james/mailbox/jpa/*/model/**/*.class</includes>
<excludes>org/apache/james/mailbox/jpa/mail/model/openjpa/EncryptDecryptHelper.class</excludes>
<addDefaultConstructor>true</addDefaultConstructor>
<enforcePropertyRestrictions>true</enforcePropertyRestrictions>
<toolProperties>
<property>
<name>log</name>
<value>TOOL=TRACE</value>
</property>
<property>
<name>metaDataFactory</name>
<value>
jpa(Types=org.apache.james.mailbox.jpa.mail.model.JPAUserFlag;
org.apache.james.mailbox.jpa.mail.model.JPAMailbox;
org.apache.james.mailbox.jpa.mail.model.openjpa.AbstractJPAMessage;
org.apache.james.mailbox.jpa.mail.model.openjpa.JPAEncryptedMessage;
org.apache.james.mailbox.jpa.mail.model.openjpa.JPAMessage;
org.apache.james.mailbox.jpa.mail.model.openjpa.JPAStreamingMessage;
org.apache.james.mailbox.jpa.mail.model.JPAProperty;
org.apache.james.mailbox.jpa.user.model.JPASubscription;
org.apache.james.mailbox.jpa.quota.model.MaxDefaultMessageCount;
org.apache.james.mailbox.jpa.quota.model.MaxDefaultStorage
org.apache.james.mailbox.jpa.quota.model.MaxUserMessageCount
org.apache.james.mailbox.jpa.quota.model.MaxUserStorage
org.apache.james.mailbox.jpa.quota.model.JpaCurrentQuota)
</value>
</property>
</toolProperties>
</configuration>
<executions>
<execution>
Expand Down
Expand Up @@ -32,6 +32,7 @@
import org.apache.james.mailbox.jpa.JPAId;
import org.apache.james.mailbox.jpa.JPATransactionalMapper;
import org.apache.james.mailbox.jpa.mail.model.JPAMailboxAnnotation;
import org.apache.james.mailbox.jpa.mail.model.JPAMailboxAnnotationId;
import org.apache.james.mailbox.model.MailboxAnnotation;
import org.apache.james.mailbox.model.MailboxAnnotationKey;
import org.apache.james.mailbox.model.MailboxId;
Expand Down Expand Up @@ -159,7 +160,7 @@ public void deleteAnnotation(MailboxId mailboxId, MailboxAnnotationKey key) {
try {
JPAId jpaId = (JPAId) mailboxId;
JPAMailboxAnnotation jpaMailboxAnnotation = getEntityManager()
.find(JPAMailboxAnnotation.class, new JPAMailboxAnnotation.JPAMailboxAnnotationId(jpaId.getRawId(), key.asString()));
.find(JPAMailboxAnnotation.class, new JPAMailboxAnnotationId(jpaId.getRawId(), key.asString()));
getEntityManager().remove(jpaMailboxAnnotation);
} catch (NoResultException e) {
LOGGER.debug("Mailbox annotation not found for ID {} and key {}", mailboxId.serialize(), key.asString());
Expand All @@ -179,7 +180,7 @@ public void insertAnnotation(MailboxId mailboxId, MailboxAnnotation mailboxAnnot
mailboxAnnotation.getValue().orNull()));
} else {
getEntityManager().find(JPAMailboxAnnotation.class,
new JPAMailboxAnnotation.JPAMailboxAnnotationId(jpaId.getRawId(), mailboxAnnotation.getKey().asString()))
new JPAMailboxAnnotationId(jpaId.getRawId(), mailboxAnnotation.getKey().asString()))
.setValue(mailboxAnnotation.getValue().orNull());
}
}
Expand All @@ -188,7 +189,7 @@ public void insertAnnotation(MailboxId mailboxId, MailboxAnnotation mailboxAnnot
public boolean exist(MailboxId mailboxId, MailboxAnnotation mailboxAnnotation) {
JPAId jpaId = (JPAId) mailboxId;
Optional<JPAMailboxAnnotation> row = Optional.fromNullable(getEntityManager().find(JPAMailboxAnnotation.class,
new JPAMailboxAnnotation.JPAMailboxAnnotationId(jpaId.getRawId(), mailboxAnnotation.getKey().asString())));
new JPAMailboxAnnotationId(jpaId.getRawId(), mailboxAnnotation.getKey().asString())));
return row.isPresent();
}

Expand Down
Expand Up @@ -78,7 +78,7 @@ public MailboxId save(Mailbox mailbox) throws MailboxException {
try {
this.lastMailboxName = mailbox.getName();
JPAMailbox persistedMailbox = JPAMailbox.from(mailbox);
getEntityManager().persist(mailbox);
getEntityManager().persist(persistedMailbox);
if (!(mailbox instanceof JPAMailbox)) {
mailbox.setMailboxId(persistedMailbox.getMailboxId());
}
Expand Down
Expand Up @@ -37,44 +37,9 @@
@NamedQuery(name = "retrieveByKey", query = "SELECT annotation FROM MailboxAnnotation annotation WHERE annotation.mailboxId = :idParam AND annotation.key = :keyParam"),
@NamedQuery(name= "countAnnotationsInMailbox", query = "SELECT COUNT(annotation) FROM MailboxAnnotation annotation WHERE annotation.mailboxId = :idParam"),
@NamedQuery(name = "retrieveByKeyLike", query = "SELECT annotation FROM MailboxAnnotation annotation WHERE annotation.mailboxId = :idParam AND annotation.key LIKE :keyParam")})
@IdClass(JPAMailboxAnnotation.JPAMailboxAnnotationId.class)
@IdClass(JPAMailboxAnnotationId.class)
public class JPAMailboxAnnotation {

public static final class JPAMailboxAnnotationId {
private long mailboxId;
private String key;

public JPAMailboxAnnotationId(long mailboxId, String key) {
this.mailboxId = mailboxId;
this.key = key;
}

public JPAMailboxAnnotationId() {
}

public long getMailboxId() {
return mailboxId;
}

public String getKey() {
return key;
}

@Override
public boolean equals(Object o) {
if (o instanceof JPAMailboxAnnotationId) {
JPAMailboxAnnotationId that = (JPAMailboxAnnotationId) o;
return Objects.equal(this.mailboxId, that.mailboxId) && Objects.equal(this.key, that.key);
}
return false;
}

@Override
public int hashCode() {
return Objects.hashCode(mailboxId, key);
}
}

public static final String MAILBOX_ID = "MAILBOX_ID";
public static final String KEY = "KEY";
public static final String VALUE = "VALUE";
Expand Down
@@ -0,0 +1,60 @@
/****************************************************************
* Licensed to the Apache Software Foundation (ASF) under one *
* or more contributor license agreements. See the NOTICE file *
* distributed with this work for additional information *
* regarding copyright ownership. The ASF licenses this file *
* to you under the Apache License, Version 2.0 (the *
* "License"); you may not use this file except in compliance *
* with the License. You may obtain a copy of the License at *
* *
* http://www.apache.org/licenses/LICENSE-2.0 *
* *
* Unless required by applicable law or agreed to in writing, *
* software distributed under the License is distributed on an *
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
* KIND, either express or implied. See the License for the *
* specific language governing permissions and limitations *
* under the License. *
****************************************************************/

package org.apache.james.mailbox.jpa.mail.model;

import com.google.common.base.Objects;

import javax.persistence.Embeddable;

@Embeddable
public final class JPAMailboxAnnotationId {
private long mailboxId;
private String key;

public JPAMailboxAnnotationId(long mailboxId, String key) {
this.mailboxId = mailboxId;
this.key = key;
}

public JPAMailboxAnnotationId() {
}

public long getMailboxId() {
return mailboxId;
}

public String getKey() {
return key;
}

@Override
public boolean equals(Object o) {
if (o instanceof JPAMailboxAnnotationId) {
JPAMailboxAnnotationId that = (JPAMailboxAnnotationId) o;
return Objects.equal(this.mailboxId, that.mailboxId) && Objects.equal(this.key, that.key);
}
return false;
}

@Override
public int hashCode() {
return Objects.hashCode(mailboxId, key);
}
}
Expand Up @@ -31,6 +31,7 @@
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Embeddable;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.IdClass;
Expand Down Expand Up @@ -98,6 +99,7 @@ public abstract class AbstractJPAMailboxMessage implements MailboxMessage {
private static final String TOSTRING_SEPARATOR = " ";

/** Identifies composite key */
@Embeddable
public static class MailboxIdUidKey implements Serializable {

private static final long serialVersionUID = 7847632032426660997L;
Expand Down
Expand Up @@ -38,7 +38,8 @@

public interface JPAMailboxFixture {

List<Class<?>> MAILBOX_PERSISTANCE_CLASSES = ImmutableList.<Class<?>>of(JPAMailbox.class,
List<Class<?>> MAILBOX_PERSISTANCE_CLASSES = ImmutableList.<Class<?>>of(
JPAMailbox.class,
AbstractJPAMailboxMessage.class,
JPAMailboxMessage.class,
JPAProperty.class,
Expand All @@ -55,11 +56,13 @@ public interface JPAMailboxFixture {
JpaCurrentQuota.class
);

List<String> MAILBOX_TABLE_NAMES = ImmutableList.<String>of("JAMES_MAIL_USERFLAG",
List<String> MAILBOX_TABLE_NAMES = ImmutableList.<String>of(
"JAMES_MAIL_USERFLAG",
"JAMES_MAIL_PROPERTY",
"JAMES_MAILBOX_ANNOTATION",
"JAMES_MAILBOX",
"JAMES_MAIL");
"JAMES_MAIL",
"JAMES_SUBSCRIPTION");

List<String> QUOTA_TABLES_NAMES = ImmutableList.<String>of(
"JAMES_MAX_DEFAULT_MESSAGE_COUNT",
Expand Down
Expand Up @@ -22,8 +22,10 @@
import java.util.List;
import java.util.Random;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

import com.google.common.base.Preconditions;
import org.apache.commons.lang.NotImplementedException;
import org.apache.james.backends.jpa.JpaTestCluster;
import org.apache.james.mailbox.MessageUid;
Expand Down

0 comments on commit e9e1f55

Please sign in to comment.