Search in sources :

Example 66 with PersistenceException

use of javax.persistence.PersistenceException in project simplejpa by appoxy.

the class Save method getEnumValue.

static String getEnumValue(PersistentProperty field, Object ob) {
    EnumType enumType = field.getEnumType();
    Class retType = field.getPropertyClass();
    Object propertyValue = field.getProperty(ob);
    String toSet = null;
    if (enumType == EnumType.STRING) {
        toSet = propertyValue.toString();
    } else {
        // ordinal
        Object[] enumConstants = retType.getEnumConstants();
        for (int i = 0; i < enumConstants.length; i++) {
            Object enumConstant = enumConstants[i];
            if (enumConstant.equals(propertyValue)) {
                toSet = Integer.toString(i);
                break;
            }
        }
    }
    if (toSet == null) {
        // should never happen
        throw new PersistenceException("Enum value is null, couldn't find ordinal match: " + ob);
    }
    return toSet;
}
Also used : EnumType(javax.persistence.EnumType) PersistenceException(javax.persistence.PersistenceException)

Example 67 with PersistenceException

use of javax.persistence.PersistenceException in project simplejpa by appoxy.

the class LazyList method loadAtleastItems.

private synchronized void loadAtleastItems(int index) {
    if ((backingList != null && nextToken == null) || (!noLimit() && index >= maxResults)) {
        return;
    }
    if (backingList == null) {
        backingList = new GrowthList();
    }
    while (backingList.size() <= index) {
        SelectResult qr;
        try {
            if (logger.isLoggable(Level.FINER))
                logger.finer("query for lazylist=" + origQuery);
            int limit = maxResults - backingList.size();
            String limitQuery = realQuery + " limit " + (noLimit() ? maxResultsPerToken : Math.min(maxResultsPerToken, limit));
            if (em.getFactory().isPrintQueries())
                System.out.println("query in lazylist=" + limitQuery);
            qr = DomainHelper.selectItems(this.em.getSimpleDb(), limitQuery, nextToken, isConsistentRead());
            if (logger.isLoggable(Level.FINER))
                logger.finer("got items for lazylist=" + qr.getItems().size());
            for (Item item : qr.getItems()) {
                backingList.add((E) em.buildObject(genericReturnType, item.getName(), item.getAttributes()));
            }
            if (qr.getNextToken() == null || (!noLimit() && qr.getItems().size() == limit)) {
                nextToken = null;
                break;
            }
            if (!noLimit() && qr.getItems().size() > limit) {
                throw new PersistenceException("Got more results than the limit.");
            }
            nextToken = qr.getNextToken();
        } catch (AmazonClientException e) {
            throw new PersistenceException("Query failed: Domain=" + domainName + " -> " + origQuery, e);
        }
    }
}
Also used : SelectResult(com.amazonaws.services.simpledb.model.SelectResult) Item(com.amazonaws.services.simpledb.model.Item) AmazonClientException(com.amazonaws.AmazonClientException) PersistenceException(javax.persistence.PersistenceException) GrowthList(org.apache.commons.collections.list.GrowthList)

Example 68 with PersistenceException

use of javax.persistence.PersistenceException in project simplejpa by appoxy.

the class EntityManagerFactoryImpl method getLibsToScan.

private static Set<String> getLibsToScan(Set<String> classNames) throws PersistenceException {
    Set<String> libs = new HashSet<String>();
    for (String className : classNames) {
        try {
            Class<?> clazz = Class.forName(className);
            URL resource = clazz.getResource(clazz.getSimpleName() + ".class");
            if (resource.getProtocol().equals("jar")) {
                libs.add(resource.getFile().split("!")[0].substring(6));
            } else if (resource.getProtocol().equals("file")) {
                libs.add(resource.getFile().substring(1));
            } else {
                throw new PersistenceException("Unknown protocol in URL: " + resource);
            }
        } catch (Throwable e) {
            throw new PersistenceException("Failed getting lib of class: " + className, e);
        }
    }
    return libs;
}
Also used : PersistenceException(javax.persistence.PersistenceException) URL(java.net.URL) HashSet(java.util.HashSet)

Example 69 with PersistenceException

use of javax.persistence.PersistenceException in project simplejpa by appoxy.

the class EntityManagerFactoryImpl method createClients.

private void createClients() {
    AWSCredentials awsCredentials = null;
    InputStream credentialsFile = getClass().getClassLoader().getResourceAsStream("AwsCredentials.properties");
    if (credentialsFile != null) {
        logger.info("Loading credentials from AwsCredentials.properties");
        try {
            awsCredentials = new PropertiesCredentials(credentialsFile);
        } catch (IOException e) {
            throw new PersistenceException("Failed loading credentials from AwsCredentials.properties.", e);
        }
    } else {
        logger.info("Loading credentials from simplejpa.properties");
        String awsAccessKey = (String) this.props.get(AWSACCESS_KEY_PROP_NAME);
        String awsSecretKey = (String) this.props.get(AWSSECRET_KEY_PROP_NAME);
        if (awsAccessKey == null || awsAccessKey.length() == 0) {
            throw new PersistenceException("AWS Access Key not found. It is a required property.");
        }
        if (awsSecretKey == null || awsSecretKey.length() == 0) {
            throw new PersistenceException("AWS Secret Key not found. It is a required property.");
        }
        awsCredentials = new BasicAWSCredentials(awsAccessKey, awsSecretKey);
    }
    this.simpleDbClient = new AmazonSimpleDBClient(awsCredentials, createConfiguration(sdbSecure));
    this.simpleDbClient.setEndpoint(sdbEndpoint);
    this.s3Client = new AmazonS3Client(awsCredentials, createConfiguration(s3Secure));
    this.s3Client.setEndpoint(s3Endpoint);
}
Also used : AmazonS3Client(com.amazonaws.services.s3.AmazonS3Client) AmazonSimpleDBClient(com.amazonaws.services.simpledb.AmazonSimpleDBClient) InputStream(java.io.InputStream) PersistenceException(javax.persistence.PersistenceException) PropertiesCredentials(com.amazonaws.auth.PropertiesCredentials) IOException(java.io.IOException) AWSCredentials(com.amazonaws.auth.AWSCredentials) BasicAWSCredentials(com.amazonaws.auth.BasicAWSCredentials) BasicAWSCredentials(com.amazonaws.auth.BasicAWSCredentials)

Example 70 with PersistenceException

use of javax.persistence.PersistenceException in project simplejpa by appoxy.

the class EntityManagerFactoryImpl method setupDbDomain.

public synchronized void setupDbDomain(String domainName) {
    try {
        if (!doesDomainExist(domainName)) {
            logger.info("creating domain: " + domainName);
            AmazonSimpleDB db = getSimpleDb();
            db.createDomain(new CreateDomainRequest().withDomainName(domainName));
            domainSet.add(domainName);
        }
    } catch (AmazonClientException e) {
        throw new PersistenceException("Could not create SimpleDB domain.", e);
    }
}
Also used : AmazonSimpleDB(com.amazonaws.services.simpledb.AmazonSimpleDB) AmazonClientException(com.amazonaws.AmazonClientException) CreateDomainRequest(com.amazonaws.services.simpledb.model.CreateDomainRequest) PersistenceException(javax.persistence.PersistenceException)

Aggregations

PersistenceException (javax.persistence.PersistenceException)125 Test (org.junit.Test)66 Session (org.hibernate.Session)50 Transaction (org.hibernate.Transaction)29 EntityManager (javax.persistence.EntityManager)17 IOException (java.io.IOException)12 StaleObjectStateException (org.hibernate.StaleObjectStateException)10 ArrayList (java.util.ArrayList)9 List (java.util.List)9 ConstraintViolationException (org.hibernate.exception.ConstraintViolationException)9 SQLGrammarException (org.hibernate.exception.SQLGrammarException)8 TransactionRequiredException (javax.persistence.TransactionRequiredException)7 MessagesEvent (com.openmeap.event.MessagesEvent)5 EntityNotFoundException (javax.persistence.EntityNotFoundException)5 OptimisticLockException (javax.persistence.OptimisticLockException)5 TestForIssue (org.hibernate.testing.TestForIssue)5 GlobalSettings (com.openmeap.model.dto.GlobalSettings)4 NoResultException (javax.persistence.NoResultException)4 LockOptions (org.hibernate.LockOptions)4 StaleStateException (org.hibernate.StaleStateException)4