mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
Compare commits
23
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6090eaa167 | ||
|
|
40eada4e80 | ||
|
|
2d375b3596 | ||
|
|
d864e2c723 | ||
|
|
e50c372562 | ||
|
|
e2cb033119 | ||
|
|
9620c57f19 | ||
|
|
07cc0fcbb6 | ||
|
|
6d1beb5c03 | ||
|
|
1667107422 | ||
|
|
de5cf1df2c | ||
|
|
a83114ced2 | ||
|
|
d6983422bf | ||
|
|
6d4b3ad84e | ||
|
|
a3df7919b5 | ||
|
|
6f1aa39cc9 | ||
|
|
f18c24c1d1 | ||
|
|
068401c29c | ||
|
|
6fbbd8b107 | ||
|
|
6379bd9e5d | ||
|
|
94d58dfa7c | ||
|
|
035fb80959 | ||
|
|
3b97f77083 |
@@ -4,3 +4,6 @@
|
||||
.settings/
|
||||
target/
|
||||
logs/
|
||||
.idea/
|
||||
*.iml
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
<?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/maven-v4_0_0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<parent>
|
||||
<groupId>org.avaje</groupId>
|
||||
<artifactId>avaje-javaparent</artifactId>
|
||||
@@ -10,14 +11,13 @@
|
||||
<groupId>org.avaje.ebeanorm</groupId>
|
||||
<artifactId>avaje-ebeanorm-spring</artifactId>
|
||||
<name>avaje-ebeanorm-spring</name>
|
||||
<version>3.2.1</version>
|
||||
<version>7.1.1</version>
|
||||
<packaging>jar</packaging>
|
||||
<description>Support for Spring transactions and IOC setup of Ebean server config</description>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<spring.framework.version>3.0.0.RELEASE</spring.framework.version>
|
||||
<ebean.mavenenhancer.version>3.2.1</ebean.mavenenhancer.version>
|
||||
<spring.framework.version>4.1.7.RELEASE</spring.framework.version>
|
||||
</properties>
|
||||
|
||||
<url>http://www.avaje.org</url>
|
||||
@@ -38,20 +38,44 @@
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>jcl-over-slf4j</artifactId>
|
||||
<version>[1.7,)</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Provided: Bring in explicitly -->
|
||||
<dependency>
|
||||
<groupId>org.avaje.ebeanorm</groupId>
|
||||
<artifactId>avaje-ebeanorm</artifactId>
|
||||
<version>[3.2,4)</version>
|
||||
<version>[6,8)</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
|
||||
<!-- Optional: Add to use AgentLoaderSupport -->
|
||||
<dependency>
|
||||
<groupId>org.avaje.ebeanorm</groupId>
|
||||
<artifactId>avaje-ebeanorm-agent</artifactId>
|
||||
<version>3.2.1</version>
|
||||
<scope>test</scope>
|
||||
<version>[4.5,5)</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- Optional: Add to use AgentLoaderSupport -->
|
||||
<dependency>
|
||||
<groupId>org.avaje</groupId>
|
||||
<artifactId>avaje-agentloader</artifactId>
|
||||
<version>2.1.2</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- Provided: Spring, bring in explicitly -->
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context</artifactId>
|
||||
<version>${spring.framework.version}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- Spring -->
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-core</artifactId>
|
||||
@@ -61,7 +85,7 @@
|
||||
<exclusion>
|
||||
<groupId>commons-logging</groupId>
|
||||
<artifactId>commons-logging</artifactId>
|
||||
</exclusion>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
@@ -73,7 +97,7 @@
|
||||
<exclusion>
|
||||
<groupId>commons-logging</groupId>
|
||||
<artifactId>commons-logging</artifactId>
|
||||
</exclusion>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
@@ -85,7 +109,7 @@
|
||||
<exclusion>
|
||||
<groupId>commons-logging</groupId>
|
||||
<artifactId>commons-logging</artifactId>
|
||||
</exclusion>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
|
||||
@@ -99,40 +123,15 @@
|
||||
<exclusion>
|
||||
<groupId>commons-logging</groupId>
|
||||
<artifactId>commons-logging</artifactId>
|
||||
</exclusion>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>jcl-over-slf4j</artifactId>
|
||||
<version>[1.6.2]</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.8.2</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.xbean</groupId>
|
||||
<artifactId>xbean-spring</artifactId>
|
||||
<version>3.5</version>
|
||||
<scope>test</scope>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>commons-logging</groupId>
|
||||
<artifactId>commons-logging</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
<version>1.2.128</version>
|
||||
<groupId>org.avaje.composite</groupId>
|
||||
<artifactId>avaje-composite-testing-ebean</artifactId>
|
||||
<version>4.1</version>
|
||||
<type>pom</type>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
@@ -143,7 +142,7 @@
|
||||
<plugin>
|
||||
<groupId>org.avaje.ebeanorm</groupId>
|
||||
<artifactId>avaje-ebeanorm-mavenenhancer</artifactId>
|
||||
<version>${ebean.mavenenhancer.version}</version>
|
||||
<version>4.10.1</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>main</id>
|
||||
@@ -159,6 +158,80 @@
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
<configuration>
|
||||
<archive>
|
||||
<manifestFile>src/main/resources/META-INF/MANIFEST.MF</manifestFile>
|
||||
</archive>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
<pluginManagement>
|
||||
<plugins>
|
||||
<!--This plugin's configuration is used to store Eclipse m2e settings
|
||||
only. It has no influence on the Maven build itself. -->
|
||||
<plugin>
|
||||
<groupId>org.eclipse.m2e</groupId>
|
||||
<artifactId>lifecycle-mapping</artifactId>
|
||||
<version>1.0.0</version>
|
||||
<configuration>
|
||||
<lifecycleMappingMetadata>
|
||||
<pluginExecutions>
|
||||
<pluginExecution>
|
||||
<pluginExecutionFilter>
|
||||
<groupId>
|
||||
org.avaje.ebeanorm
|
||||
</groupId>
|
||||
<artifactId>
|
||||
avaje-ebeanorm-mavenenhancer
|
||||
</artifactId>
|
||||
<versionRange>
|
||||
[3,4)
|
||||
</versionRange>
|
||||
<goals>
|
||||
<goal>enhance</goal>
|
||||
</goals>
|
||||
</pluginExecutionFilter>
|
||||
<action>
|
||||
<ignore />
|
||||
</action>
|
||||
</pluginExecution>
|
||||
</pluginExecutions>
|
||||
</lifecycleMappingMetadata>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-javadoc-plugin</artifactId>
|
||||
<version>2.9.1</version>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<doclet>org.avaje.doclet.PygmentsDoclet</doclet>
|
||||
<docletArtifact>
|
||||
<groupId>org.avaje</groupId>
|
||||
<artifactId>pygments-doclet</artifactId>
|
||||
<version>1.0.0</version>
|
||||
</docletArtifact>
|
||||
<additionalparam>
|
||||
-Xdoclint:none
|
||||
</additionalparam>
|
||||
<linksource>true</linksource>
|
||||
</configuration>
|
||||
|
||||
<executions>
|
||||
<execution>
|
||||
<id>attach-javadocs</id>
|
||||
<goals>
|
||||
<goal>jar</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
|
||||
</plugins>
|
||||
</pluginManagement>
|
||||
</build>
|
||||
</project>
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
package com.avaje.ebean.springsupport;
|
||||
|
||||
import org.avaje.agentloader.AgentLoader;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
|
||||
/**
|
||||
* To Setup Enhancement in Spring
|
||||
* <bean class="org.ebean.AgentLoaderSupport">
|
||||
* <property name="debug" value="1" />
|
||||
* <property name="packages" value="org.ebean.**" />
|
||||
* </bean>
|
||||
* Created by guor on 2015/5/11.
|
||||
*/
|
||||
public class AgentLoaderSupport implements InitializingBean {
|
||||
|
||||
private Logger logger = LoggerFactory.getLogger(getClass());
|
||||
|
||||
private int debug;
|
||||
private String packages;
|
||||
|
||||
public int getDebug() {
|
||||
return debug;
|
||||
}
|
||||
|
||||
public void setDebug(int debug) {
|
||||
this.debug = debug;
|
||||
}
|
||||
|
||||
public String getPackages() {
|
||||
return packages;
|
||||
}
|
||||
|
||||
public void setPackages(String packages) {
|
||||
this.packages = packages;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
String args = "debug=" + getDebug() + ";packages=" + getPackages();
|
||||
if (!AgentLoader.loadAgentFromClasspath("avaje-ebeanorm-agent", args)) {
|
||||
logger.info("avaje-ebeanorm-agent not found in classpath - not dynamically loaded");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,18 +1,18 @@
|
||||
/**
|
||||
* Copyright (C) 2009 the original author or authors
|
||||
*
|
||||
* <p>
|
||||
* This file is part of Ebean.
|
||||
*
|
||||
* <p>
|
||||
* Ebean is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation; either version 2.1 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* <p>
|
||||
* Ebean is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* <p>
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
@@ -34,52 +34,52 @@ import com.avaje.ebean.config.ServerConfig;
|
||||
*/
|
||||
public class EbeanServerFactoryBean implements InitializingBean, FactoryBean<EbeanServer> {
|
||||
|
||||
/**
|
||||
* The Ebean server configuration.
|
||||
*/
|
||||
private ServerConfig serverConfig;
|
||||
/**
|
||||
* The Ebean server configuration.
|
||||
*/
|
||||
private ServerConfig serverConfig;
|
||||
|
||||
/**
|
||||
* The EbeanServer instance.
|
||||
*/
|
||||
private EbeanServer ebeanServer;
|
||||
/**
|
||||
* The EbeanServer instance.
|
||||
*/
|
||||
private EbeanServer ebeanServer;
|
||||
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
|
||||
if (serverConfig == null){
|
||||
throw new Exception("No ServerConig set. You must define a ServerConfig bean");
|
||||
}
|
||||
if (serverConfig == null) {
|
||||
throw new Exception("No ServerConig set. You must define a ServerConfig bean");
|
||||
}
|
||||
|
||||
// Create the new EbeanServer using the configuration
|
||||
this.ebeanServer = EbeanServerFactory.create(serverConfig);
|
||||
}
|
||||
// Create the new EbeanServer using the configuration
|
||||
this.ebeanServer = EbeanServerFactory.create(serverConfig);
|
||||
}
|
||||
|
||||
public EbeanServer getObject() throws Exception {
|
||||
return ebeanServer;
|
||||
}
|
||||
public EbeanServer getObject() throws Exception {
|
||||
return ebeanServer;
|
||||
}
|
||||
|
||||
public Class<? extends EbeanServer> getObjectType() {
|
||||
return EbeanServer.class;
|
||||
}
|
||||
public Class<? extends EbeanServer> getObjectType() {
|
||||
return EbeanServer.class;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true for EbeanServer.
|
||||
*/
|
||||
public boolean isSingleton() {
|
||||
return true;
|
||||
}
|
||||
/**
|
||||
* Returns true for EbeanServer.
|
||||
*/
|
||||
public boolean isSingleton() {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the server configuration.
|
||||
*/
|
||||
public ServerConfig getServerConfig() {
|
||||
return serverConfig;
|
||||
}
|
||||
/**
|
||||
* Return the server configuration.
|
||||
*/
|
||||
public ServerConfig getServerConfig() {
|
||||
return serverConfig;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the server configuration.
|
||||
*/
|
||||
public void setServerConfig(ServerConfig serverConfig) {
|
||||
this.serverConfig = serverConfig;
|
||||
}
|
||||
/**
|
||||
* Set the server configuration.
|
||||
*/
|
||||
public void setServerConfig(ServerConfig serverConfig) {
|
||||
this.serverConfig = serverConfig;
|
||||
}
|
||||
}
|
||||
|
||||
+172
-172
@@ -1,18 +1,18 @@
|
||||
/**
|
||||
* Copyright (C) 2009 the original author or authors
|
||||
*
|
||||
* <p>
|
||||
* This file is part of Ebean.
|
||||
*
|
||||
* <p>
|
||||
* Ebean is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation; either version 2.1 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* <p>
|
||||
* Ebean is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* <p>
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
@@ -38,195 +38,195 @@ import com.avaje.ebeaninternal.server.transaction.TransactionManager;
|
||||
|
||||
/**
|
||||
* A Spring aware TransactionScopeManager.
|
||||
*
|
||||
*
|
||||
* <p>
|
||||
* Will look for Spring transactions and use them if they exist.
|
||||
* </p>
|
||||
*
|
||||
*
|
||||
* @since 18.05.2009
|
||||
* @author E Mc Greal
|
||||
*/
|
||||
public class SpringAwareJdbcTransactionManager implements ExternalTransactionManager {
|
||||
|
||||
private final static Logger logger = Logger.getLogger(SpringAwareJdbcTransactionManager.class.getName());
|
||||
private final static Logger logger = Logger.getLogger(SpringAwareJdbcTransactionManager.class.getName());
|
||||
|
||||
/**
|
||||
* The data source.
|
||||
*/
|
||||
private DataSource dataSource;
|
||||
/**
|
||||
* The data source.
|
||||
*/
|
||||
private DataSource dataSource;
|
||||
|
||||
/**
|
||||
* The Ebean transaction manager.
|
||||
*/
|
||||
private TransactionManager transactionManager;
|
||||
/**
|
||||
* The Ebean transaction manager.
|
||||
*/
|
||||
private TransactionManager transactionManager;
|
||||
|
||||
/**
|
||||
* The EbeanServer name.
|
||||
*/
|
||||
private String serverName;
|
||||
|
||||
/**
|
||||
* Instantiates a new spring aware transaction scope manager.
|
||||
*/
|
||||
public SpringAwareJdbcTransactionManager() {
|
||||
/**
|
||||
* The EbeanServer name.
|
||||
*/
|
||||
private String serverName;
|
||||
|
||||
/**
|
||||
* Instantiates a new spring aware transaction scope manager.
|
||||
*/
|
||||
public SpringAwareJdbcTransactionManager() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialise this with the Ebean internal transaction manager.
|
||||
*/
|
||||
public void setTransactionManager(Object txnMgr) {
|
||||
|
||||
// RB: At this stage not exposing TransactionManager to
|
||||
// the public API and hence the Object type and casting here
|
||||
|
||||
this.transactionManager = (TransactionManager) txnMgr;
|
||||
this.dataSource = transactionManager.getDataSource();
|
||||
this.serverName = transactionManager.getServerName();
|
||||
}
|
||||
|
||||
/**
|
||||
* Looks for a current Spring managed transaction and wraps/returns that as a Ebean transaction.
|
||||
* <p>
|
||||
* Returns null if there is no current spring transaction (lazy loading outside a spring txn etc).
|
||||
* </p>
|
||||
*/
|
||||
public Object getCurrentTransaction() {
|
||||
|
||||
// Get the current Spring ConnectionHolder associated to the current spring managed transaction
|
||||
ConnectionHolder holder = (ConnectionHolder) TransactionSynchronizationManager.getResource(dataSource);
|
||||
|
||||
if (holder == null || !holder.isSynchronizedWithTransaction()) {
|
||||
// no current Spring transaction
|
||||
SpiTransaction currentEbeanTransaction = DefaultTransactionThreadLocal.get(serverName);
|
||||
if (currentEbeanTransaction != null) {
|
||||
// NOT expecting this so log WARNING
|
||||
String msg = "SpringTransaction - no current spring txn BUT using current Ebean one " + currentEbeanTransaction.getId();
|
||||
logger.log(Level.WARNING, msg);
|
||||
|
||||
} else if (logger.isLoggable(Level.FINEST)) {
|
||||
logger.log(Level.FINEST, "Spring Txn - no current transaction ");
|
||||
}
|
||||
return currentEbeanTransaction;
|
||||
}
|
||||
|
||||
SpringTxnListener springTxnLister = getSpringTxnListener();
|
||||
|
||||
if (springTxnLister != null) {
|
||||
// we have already seen this transaction
|
||||
return springTxnLister.getTransaction();
|
||||
|
||||
} else {
|
||||
// This is a new spring transaction that we have not seen before.
|
||||
// "wrap" it in a SpringJdbcTransaction for use with Ebean
|
||||
SpringJdbcTransaction newTrans = new SpringJdbcTransaction(holder, transactionManager);
|
||||
|
||||
// Create and register a Spring TransactionSynchronization for this transaction
|
||||
springTxnLister = createSpringTxnListener(newTrans);
|
||||
TransactionSynchronizationManager.registerSynchronization(springTxnLister);
|
||||
|
||||
// also put in Ebean ThreadLocal
|
||||
DefaultTransactionThreadLocal.set(serverName, newTrans);
|
||||
return newTrans;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Search for our specific transaction listener.
|
||||
* <p>
|
||||
* If it exists then we have already seen and "wrapped" this transaction.
|
||||
* </p>
|
||||
*/
|
||||
private SpringTxnListener getSpringTxnListener() {
|
||||
|
||||
if (TransactionSynchronizationManager.isSynchronizationActive()) {
|
||||
List<TransactionSynchronization> synchronizations = TransactionSynchronizationManager.getSynchronizations();
|
||||
if (synchronizations != null) {
|
||||
// search for our specific listener
|
||||
for (int i = 0; i < synchronizations.size(); i++) {
|
||||
if (synchronizations.get(i) instanceof SpringTxnListener) {
|
||||
return (SpringTxnListener) synchronizations.get(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a listener to register with Spring to enable Ebean to be
|
||||
* notified when transactions commit and rollback.
|
||||
* <p>
|
||||
* This is used by Ebean to notify it's appropriate listeners and maintain it's server
|
||||
* cache etc.
|
||||
* </p>
|
||||
*/
|
||||
private SpringTxnListener createSpringTxnListener(SpringJdbcTransaction t) {
|
||||
return new SpringTxnListener(transactionManager, t);
|
||||
}
|
||||
|
||||
/**
|
||||
* A Spring TransactionSynchronization that we register with Spring to get
|
||||
* notified when a Spring managed transaction has been committed or rolled
|
||||
* back.
|
||||
* <p>
|
||||
* When Ebean is notified (of the commit/rollback) it can then manage its
|
||||
* cache, notify BeanPersistListeners etc.
|
||||
* </p>
|
||||
*/
|
||||
private static class SpringTxnListener extends TransactionSynchronizationAdapter {
|
||||
|
||||
private final TransactionManager transactionManager;
|
||||
|
||||
private final SpringJdbcTransaction transaction;
|
||||
|
||||
private final String serverName;
|
||||
|
||||
private SpringTxnListener(TransactionManager transactionManager, SpringJdbcTransaction t) {
|
||||
this.transactionManager = transactionManager;
|
||||
this.transaction = t;
|
||||
this.serverName = transactionManager.getServerName();
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialise this with the Ebean internal transaction manager.
|
||||
* Return the associated Ebean wrapped transaction.
|
||||
*/
|
||||
public void setTransactionManager(Object txnMgr) {
|
||||
|
||||
// RB: At this stage not exposing TransactionManager to
|
||||
// the public API and hence the Object type and casting here
|
||||
|
||||
this.transactionManager = (TransactionManager) txnMgr;
|
||||
this.dataSource = transactionManager.getDataSource();
|
||||
this.serverName = transactionManager.getServerName();
|
||||
public SpringJdbcTransaction getTransaction() {
|
||||
return transaction;
|
||||
}
|
||||
|
||||
/**
|
||||
* Looks for a current Spring managed transaction and wraps/returns that as a Ebean transaction.
|
||||
* <p>
|
||||
* Returns null if there is no current spring transaction (lazy loading outside a spring txn etc).
|
||||
* </p>
|
||||
*/
|
||||
public Object getCurrentTransaction() {
|
||||
|
||||
// Get the current Spring ConnectionHolder associated to the current spring managed transaction
|
||||
ConnectionHolder holder = (ConnectionHolder) TransactionSynchronizationManager.getResource(dataSource);
|
||||
|
||||
if (holder == null || !holder.isSynchronizedWithTransaction()) {
|
||||
// no current Spring transaction
|
||||
SpiTransaction currentEbeanTransaction = DefaultTransactionThreadLocal.get(serverName);
|
||||
if (currentEbeanTransaction != null){
|
||||
// NOT expecting this so log WARNING
|
||||
String msg = "SpringTransaction - no current spring txn BUT using current Ebean one "+currentEbeanTransaction.getId();
|
||||
logger.log(Level.WARNING, msg);
|
||||
|
||||
} else if (logger.isLoggable(Level.FINEST)) {
|
||||
logger.log(Level.FINEST, "Spring Txn - no current transaction ");
|
||||
}
|
||||
return currentEbeanTransaction;
|
||||
}
|
||||
|
||||
SpringTxnListener springTxnLister = getSpringTxnListener();
|
||||
|
||||
if (springTxnLister != null){
|
||||
// we have already seen this transaction
|
||||
return springTxnLister.getTransaction();
|
||||
|
||||
} else {
|
||||
// This is a new spring transaction that we have not seen before.
|
||||
// "wrap" it in a SpringJdbcTransaction for use with Ebean
|
||||
SpringJdbcTransaction newTrans = new SpringJdbcTransaction(holder, transactionManager);
|
||||
|
||||
// Create and register a Spring TransactionSynchronization for this transaction
|
||||
springTxnLister = createSpringTxnListener(newTrans);
|
||||
TransactionSynchronizationManager.registerSynchronization(springTxnLister);
|
||||
|
||||
// also put in Ebean ThreadLocal
|
||||
DefaultTransactionThreadLocal.set(serverName, newTrans);
|
||||
return newTrans;
|
||||
}
|
||||
@Override
|
||||
public void beforeCommit(boolean readOnly) {
|
||||
// Future note: for JPA2 locking we will
|
||||
// have beforeCommit events to fire
|
||||
}
|
||||
|
||||
/**
|
||||
* Search for our specific transaction listener.
|
||||
* <p>
|
||||
* If it exists then we have already seen and "wrapped" this transaction.
|
||||
* </p>
|
||||
*/
|
||||
private SpringTxnListener getSpringTxnListener() {
|
||||
@Override
|
||||
public void afterCompletion(int status) {
|
||||
|
||||
if (TransactionSynchronizationManager.isSynchronizationActive()){
|
||||
List<TransactionSynchronization> synchronizations = TransactionSynchronizationManager.getSynchronizations();
|
||||
if (synchronizations != null){
|
||||
// search for our specific listener
|
||||
for (int i = 0; i < synchronizations.size(); i++) {
|
||||
if (synchronizations.get(i) instanceof SpringTxnListener){
|
||||
return (SpringTxnListener)synchronizations.get(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a listener to register with Spring to enable Ebean to be
|
||||
* notified when transactions commit and rollback.
|
||||
* <p>
|
||||
* This is used by Ebean to notify it's appropriate listeners and maintain it's server
|
||||
* cache etc.
|
||||
* </p>
|
||||
*/
|
||||
private SpringTxnListener createSpringTxnListener(SpringJdbcTransaction t) {
|
||||
return new SpringTxnListener(transactionManager, t);
|
||||
}
|
||||
switch (status) {
|
||||
case STATUS_COMMITTED:
|
||||
if (logger.isLoggable(Level.FINE)) {
|
||||
logger.fine("Spring Txn [" + transaction.getId() + "] committed");
|
||||
}
|
||||
transactionManager.notifyOfCommit(transaction);
|
||||
break;
|
||||
|
||||
/**
|
||||
* A Spring TransactionSynchronization that we register with Spring to get
|
||||
* notified when a Spring managed transaction has been committed or rolled
|
||||
* back.
|
||||
* <p>
|
||||
* When Ebean is notified (of the commit/rollback) it can then manage its
|
||||
* cache, notify BeanPersistListeners etc.
|
||||
* </p>
|
||||
*/
|
||||
private static class SpringTxnListener extends TransactionSynchronizationAdapter {
|
||||
|
||||
private final TransactionManager transactionManager;
|
||||
|
||||
private final SpringJdbcTransaction transaction;
|
||||
|
||||
private final String serverName;
|
||||
|
||||
private SpringTxnListener(TransactionManager transactionManager, SpringJdbcTransaction t){
|
||||
this.transactionManager = transactionManager;
|
||||
this.transaction = t;
|
||||
this.serverName = transactionManager.getServerName();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the associated Ebean wrapped transaction.
|
||||
*/
|
||||
public SpringJdbcTransaction getTransaction() {
|
||||
return transaction;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void beforeCommit(boolean readOnly) {
|
||||
// Future note: for JPA2 locking we will
|
||||
// have beforeCommit events to fire
|
||||
}
|
||||
case STATUS_ROLLED_BACK:
|
||||
if (logger.isLoggable(Level.FINE)) {
|
||||
logger.fine("Spring Txn [" + transaction.getId() + "] rollback");
|
||||
}
|
||||
transactionManager.notifyOfRollback(transaction, null);
|
||||
break;
|
||||
|
||||
@Override
|
||||
public void afterCompletion(int status) {
|
||||
|
||||
switch (status) {
|
||||
case STATUS_COMMITTED:
|
||||
if (logger.isLoggable(Level.FINE)){
|
||||
logger.fine("Spring Txn ["+transaction.getId()+"] committed");
|
||||
}
|
||||
transactionManager.notifyOfCommit(transaction);
|
||||
break;
|
||||
|
||||
case STATUS_ROLLED_BACK:
|
||||
if (logger.isLoggable(Level.FINE)){
|
||||
logger.fine("Spring Txn ["+transaction.getId()+"] rollback");
|
||||
}
|
||||
transactionManager.notifyOfRollback(transaction, null);
|
||||
break;
|
||||
|
||||
default:
|
||||
// this should never happen
|
||||
String msg = "Invalid status "+status;
|
||||
throw new PersistenceException(msg);
|
||||
}
|
||||
|
||||
// Remove this transaction object as it is completed
|
||||
DefaultTransactionThreadLocal.replace(serverName, null);
|
||||
}
|
||||
default:
|
||||
// this should never happen
|
||||
String msg = "Invalid status " + status;
|
||||
throw new PersistenceException(msg);
|
||||
}
|
||||
|
||||
// Remove this transaction object as it is completed
|
||||
DefaultTransactionThreadLocal.replace(serverName, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,18 +1,18 @@
|
||||
/**
|
||||
* Copyright (C) 2009 the original author or authors
|
||||
*
|
||||
* <p>
|
||||
* This file is part of Ebean.
|
||||
*
|
||||
* <p>
|
||||
* Ebean is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation; either version 2.1 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* <p>
|
||||
* Ebean is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* <p>
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
@@ -27,19 +27,19 @@ import com.avaje.ebeaninternal.server.transaction.TransactionManager;
|
||||
|
||||
public class SpringJdbcTransaction extends ExternalJdbcTransaction {
|
||||
|
||||
private final ConnectionHolder holder;
|
||||
private final ConnectionHolder holder;
|
||||
|
||||
public SpringJdbcTransaction(ConnectionHolder holder, TransactionManager manager) {
|
||||
super("s"+holder.hashCode(), true, holder.getConnection(), manager);
|
||||
this.holder = holder;
|
||||
}
|
||||
public SpringJdbcTransaction(ConnectionHolder holder, TransactionManager manager) {
|
||||
super("s" + holder.hashCode(), true, holder.getConnection(), manager);
|
||||
this.holder = holder;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isActive() {
|
||||
return holder.isSynchronizedWithTransaction();
|
||||
}
|
||||
@Override
|
||||
public boolean isActive() {
|
||||
return holder.isSynchronizedWithTransaction();
|
||||
}
|
||||
|
||||
public ConnectionHolder getConnectionHolder() {
|
||||
return holder;
|
||||
}
|
||||
public ConnectionHolder getConnectionHolder() {
|
||||
return holder;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
Manifest-Version: 1.0
|
||||
Premain-Class: com.avaje.ebean.enhance.agent.Transformer
|
||||
Bundle-ManifestVersion: 2
|
||||
Bundle-Name: Ebean-ORM-SPRING
|
||||
Bundle-SymbolicName: com.avaje.ebean.springsupport
|
||||
Bundle-Version: 4.1.9
|
||||
Bundle-ClassPath: .
|
||||
Bundle-Vendor: avaje
|
||||
Export-Package: com.avaje.ebean.springsupport.factory,
|
||||
com.avaje.ebean.springsupport.txn
|
||||
Bundle-RequiredExecutionEnvironment: JavaSE-1.7
|
||||
Require-Bundle: com.avaje.ebean;bundle-version="4.1.9",
|
||||
org.springframework.beans;bundle-version="3.1.0",
|
||||
org.springframework.context;bundle-version="3.1.0",
|
||||
org.slf4j.api;bundle-version="1.7.2"
|
||||
Import-Package: javax.persistence;version="2.0.0"
|
||||
|
||||
|
||||
@@ -1,27 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:context="http://www.springframework.org/schema/context"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
|
||||
http://www.springframework.org/schema/context
|
||||
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd">
|
||||
|
||||
<!-- Default abstract Ebean Server configuration -->
|
||||
<bean id="defaultEbeanServerConfig" class="com.avaje.ebean.config.ServerConfig" abstract="true">
|
||||
<property name="autofetchConfig">
|
||||
<bean class="com.avaje.ebean.config.AutofetchConfig">
|
||||
<property name="logDirectory" value="logs"/>
|
||||
</bean>
|
||||
</property>
|
||||
|
||||
<!-- turn off all logging by default -->
|
||||
<property name="loggingDirectory" value="logs"/>
|
||||
<property name="loggingLevel" value="NONE"/>
|
||||
|
||||
<property name="externalTransactionManager">
|
||||
<bean class="com.avaje.ebean.springsupport.txn.SpringAwareJdbcTransactionManager"/>
|
||||
</property>
|
||||
|
||||
<property name="namingConvention">
|
||||
<bean class="com.avaje.ebean.config.UnderscoreNamingConvention"/>
|
||||
</property>
|
||||
|
||||
@@ -1,18 +1,18 @@
|
||||
/**
|
||||
* Copyright (C) 2009 the original author or authors
|
||||
*
|
||||
* <p>
|
||||
* This file is part of Ebean.
|
||||
*
|
||||
* <p>
|
||||
* Ebean is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation; either version 2.1 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* <p>
|
||||
* Ebean is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* <p>
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
@@ -31,47 +31,27 @@ import javax.persistence.OneToOne;
|
||||
@Entity
|
||||
public class Account {
|
||||
|
||||
/** The oid. */
|
||||
@Id
|
||||
private long oid;
|
||||
/** The id. */
|
||||
@Id
|
||||
private Long id;
|
||||
|
||||
/** The user. */
|
||||
@OneToOne
|
||||
private User user;
|
||||
/** The user. */
|
||||
@OneToOne
|
||||
private User user;
|
||||
|
||||
/**
|
||||
* Gets the oid.
|
||||
*
|
||||
* @return the oid
|
||||
*/
|
||||
public long getOid() {
|
||||
return oid;
|
||||
}
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the oid.
|
||||
*
|
||||
* @param oid the oid to set
|
||||
*/
|
||||
public void setOid(long oid) {
|
||||
this.oid = oid;
|
||||
}
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the user.
|
||||
*
|
||||
* @return the user
|
||||
*/
|
||||
public User getUser() {
|
||||
return user;
|
||||
}
|
||||
public User getUser() {
|
||||
return user;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the user.
|
||||
*
|
||||
* @param user the user to set
|
||||
*/
|
||||
public void setUser(User user) {
|
||||
this.user = user;
|
||||
}
|
||||
public void setUser(User user) {
|
||||
this.user = user;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,18 +1,18 @@
|
||||
/**
|
||||
* Copyright (C) 2009 the original author or authors
|
||||
*
|
||||
* <p>
|
||||
* This file is part of Ebean.
|
||||
*
|
||||
* <p>
|
||||
* Ebean is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation; either version 2.1 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* <p>
|
||||
* Ebean is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* <p>
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
@@ -25,74 +25,79 @@ import static org.junit.Assert.assertTrue;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
/**
|
||||
* Unit test for Ebean Spring Module.
|
||||
* @since 18.05.2009
|
||||
* @author E Mc Greal
|
||||
*/
|
||||
@ContextConfiguration(locations={"/init-database.xml"})
|
||||
public class EbeanSpringModuleTest extends AbstractJUnit4SpringContextTests {
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations = {"/init-database.xml"})
|
||||
public class EbeanSpringModuleTest {
|
||||
|
||||
/** The Constant logger. */
|
||||
private final static Logger logger = Logger.getLogger(EbeanSpringModuleTest.class.getName());
|
||||
/** The Constant logger. */
|
||||
private final static Logger logger = Logger.getLogger(EbeanSpringModuleTest.class.getName());
|
||||
|
||||
/** The user service. */
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
/** The user service. */
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
|
||||
/**
|
||||
* Create the test case.
|
||||
*/
|
||||
public EbeanSpringModuleTest(){
|
||||
super();
|
||||
}
|
||||
/**
|
||||
* Create the test case.
|
||||
*/
|
||||
public EbeanSpringModuleTest() {
|
||||
super();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Test app.
|
||||
*/
|
||||
@Test
|
||||
public void testSaveUser(){
|
||||
logger.info("Saving new User...");
|
||||
User user = new User();
|
||||
user.setName("ebean");
|
||||
userService.save(user);
|
||||
logger.info("Saved new User");
|
||||
}
|
||||
@Test
|
||||
public void testBatchInsert() {
|
||||
|
||||
/**
|
||||
* Test app.
|
||||
*/
|
||||
@Test
|
||||
public void testFindUser(){
|
||||
logger.info("Finding User with OID = 1 ...");
|
||||
User user = userService.find(1);
|
||||
userService.batchInsert();
|
||||
}
|
||||
|
||||
assertNotNull(user);
|
||||
assertTrue("ebean".equals(user.getName()));
|
||||
logger.info("Found User with OID = 1");
|
||||
}
|
||||
/**
|
||||
* Test app.
|
||||
*/
|
||||
@Test
|
||||
public void testSaveUser() {
|
||||
logger.info("Saving new User...");
|
||||
User user = new User();
|
||||
user.setName("ebean");
|
||||
userService.save(user);
|
||||
logger.info("Saved new User");
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the user service.
|
||||
*
|
||||
* @return the userService
|
||||
*/
|
||||
public UserService getUserService() {
|
||||
return userService;
|
||||
}
|
||||
/**
|
||||
* Test app.
|
||||
*/
|
||||
@Test
|
||||
public void testFindUser() {
|
||||
logger.info("Finding User with OID = 1 ...");
|
||||
User user = userService.find(1);
|
||||
|
||||
assertNotNull(user);
|
||||
assertTrue("ebean".equals(user.getName()));
|
||||
logger.info("Found User with OID = 1");
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the user service.
|
||||
*/
|
||||
public UserService getUserService() {
|
||||
return userService;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Sets the user service.
|
||||
*
|
||||
* @param userService the userService to set
|
||||
*/
|
||||
public void setUserService(UserService userService) {
|
||||
this.userService = userService;
|
||||
}
|
||||
/**
|
||||
* Sets the user service.
|
||||
*/
|
||||
public void setUserService(UserService userService) {
|
||||
this.userService = userService;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,18 +1,18 @@
|
||||
/**
|
||||
* Copyright (C) 2009 the original author or authors
|
||||
*
|
||||
* <p>
|
||||
* This file is part of Ebean.
|
||||
*
|
||||
* <p>
|
||||
* Ebean is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation; either version 2.1 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* <p>
|
||||
* Ebean is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* <p>
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
@@ -34,40 +34,28 @@ import javax.persistence.ManyToMany;
|
||||
*/
|
||||
@Entity
|
||||
public class Role {
|
||||
@Id
|
||||
long oid;
|
||||
|
||||
@ManyToMany
|
||||
private Set<User> users = new HashSet<User>();
|
||||
@Id
|
||||
Long id;
|
||||
|
||||
/**
|
||||
* @return the oid
|
||||
*/
|
||||
public long getOid() {
|
||||
return oid;
|
||||
}
|
||||
@ManyToMany
|
||||
private Set<User> users;
|
||||
|
||||
/**
|
||||
* @param oid the oid to set
|
||||
*/
|
||||
public void setOid(long oid) {
|
||||
this.oid = oid;
|
||||
}
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the users
|
||||
*/
|
||||
public Set<User> getUsers() {
|
||||
return users;
|
||||
}
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param users the users to set
|
||||
*/
|
||||
public void setUsers(Set<User> users) {
|
||||
this.users = users;
|
||||
}
|
||||
public Set<User> getUsers() {
|
||||
return users;
|
||||
}
|
||||
|
||||
public void setUsers(Set<User> users) {
|
||||
this.users = users;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -17,16 +17,13 @@
|
||||
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
package com.avaje.test.springsupport;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.ManyToMany;
|
||||
import javax.persistence.OneToOne;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* The Class User.
|
||||
@@ -41,63 +38,39 @@ public class User {
|
||||
private String name;
|
||||
|
||||
@ManyToMany
|
||||
private Set<Role> roles = new HashSet<Role>();
|
||||
private Set<Role> roles;
|
||||
|
||||
@OneToOne(mappedBy="user")
|
||||
private Account account;
|
||||
|
||||
/**
|
||||
* @return the oid
|
||||
*/
|
||||
public long getOid() {
|
||||
return oid;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param oid the oid to set
|
||||
*/
|
||||
public void setOid(long oid) {
|
||||
this.oid = oid;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the roles
|
||||
*/
|
||||
public Set<Role> getRoles() {
|
||||
return roles;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param roles the roles to set
|
||||
*/
|
||||
public void setRoles(Set<Role> roles) {
|
||||
this.roles = roles;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the name
|
||||
*/
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param name the name to set
|
||||
*/
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the account
|
||||
*/
|
||||
public Account getAccount() {
|
||||
return account;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param account the account to set
|
||||
*/
|
||||
public void setAccount(Account account) {
|
||||
this.account = account;
|
||||
}
|
||||
|
||||
@@ -26,6 +26,10 @@ package com.avaje.test.springsupport;
|
||||
* @author E Mc Greal
|
||||
*/
|
||||
public interface UserService {
|
||||
public void save(User user);
|
||||
public User find(long oid);
|
||||
|
||||
void save(User user);
|
||||
|
||||
User find(long id);
|
||||
|
||||
void batchInsert();
|
||||
}
|
||||
|
||||
@@ -20,12 +20,17 @@
|
||||
|
||||
package com.avaje.test.springsupport;
|
||||
|
||||
import com.avaje.ebean.Transaction;
|
||||
import com.avaje.ebean.config.PersistBatch;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.transaction.annotation.Propagation;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import com.avaje.ebean.EbeanServer;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* The Class UserServiceImpl.
|
||||
*
|
||||
@@ -47,15 +52,35 @@ public class UserServiceImpl implements UserService {
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true, propagation = Propagation.REQUIRED)
|
||||
public User find(long oid) {
|
||||
return ebeanServer.find(User.class, oid);
|
||||
public User find(long id) {
|
||||
return ebeanServer.find(User.class, id);
|
||||
}
|
||||
|
||||
|
||||
@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
|
||||
public void batchInsert(){
|
||||
|
||||
List<User> users = new ArrayList<User>();
|
||||
for(int i=0 ;i<5;i++){
|
||||
User user = new User();
|
||||
user.setName("user"+i);
|
||||
users.add(user);
|
||||
}
|
||||
|
||||
System.out.println("---------before batch-------");
|
||||
|
||||
Transaction tx = ebeanServer.beginTransaction();
|
||||
tx.setBatch(PersistBatch.NONE);
|
||||
tx.setBatchOnCascade(PersistBatch.ALL);
|
||||
tx.setBatchSize(20);
|
||||
ebeanServer.saveAll(users);//
|
||||
|
||||
System.out.println("---------after batch-------");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Gets the ebean server.
|
||||
*
|
||||
* @return the ebeanServer
|
||||
* Return the ebean server.
|
||||
*/
|
||||
public EbeanServer getEbeanServer() {
|
||||
return ebeanServer;
|
||||
@@ -63,8 +88,6 @@ public class UserServiceImpl implements UserService {
|
||||
|
||||
/**
|
||||
* Sets the ebean server.
|
||||
*
|
||||
* @param ebeanServer the ebeanServer to set
|
||||
*/
|
||||
public void setEbeanServer(EbeanServer ebeanServer) {
|
||||
this.ebeanServer = ebeanServer;
|
||||
|
||||
@@ -14,11 +14,15 @@
|
||||
<tx:annotation-driven transaction-manager="transactionManager" />
|
||||
<context:annotation-config />
|
||||
|
||||
<!-- The real DataSource -->
|
||||
<bean id="dataSource"
|
||||
class="org.springframework.jndi.JndiObjectFactoryBean">
|
||||
<property name="jndiName" value="java:comp/env/jdbc/ebeantest" />
|
||||
</bean>
|
||||
<bean id="dataSource" class="org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy">
|
||||
<constructor-arg>
|
||||
<bean class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
|
||||
<property name="driverClass" value="org.h2.Driver" />
|
||||
<property name="url"
|
||||
value="jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1" />
|
||||
</bean>
|
||||
</constructor-arg>
|
||||
</bean>
|
||||
|
||||
<!-- Transaction Manager -->
|
||||
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
java.naming.factory.initial = org.apache.xbean.spring.jndi.SpringInitialContextFactory
|
||||
@@ -1,25 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
|
||||
|
||||
<beans>
|
||||
<bean id="jndi"
|
||||
class="org.apache.xbean.spring.jndi.DefaultContext">
|
||||
<property name="entries">
|
||||
<map>
|
||||
<entry key="java:comp/env/jdbc/ebeantest">
|
||||
<bean
|
||||
class="org.springframework.jdbc.datasource.SingleConnectionDataSource"
|
||||
destroy-method="close" singleton="false">
|
||||
<property name="driverClassName"
|
||||
value="org.h2.Driver" />
|
||||
<property name="url" value="jdbc:h2:mem:tests;DB_CLOSE_DELAY=-1" />
|
||||
<property name="username" value="ebean" />
|
||||
<property name="password" value="ebean" />
|
||||
<property name="autoCommit" value="false" />
|
||||
<property name="suppressClose" value="true" />
|
||||
</bean>
|
||||
</entry>
|
||||
</map>
|
||||
</property>
|
||||
</bean>
|
||||
</beans>
|
||||
Reference in New Issue
Block a user