diff --git a/.gitignore b/.gitignore new file mode 100644 index 000000000..77344b780 --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +*.sql +.classpath +.project +.settings/ +target/ +logs/ diff --git a/pom.xml b/pom.xml new file mode 100644 index 000000000..f6c0213af --- /dev/null +++ b/pom.xml @@ -0,0 +1,130 @@ + + + 4.0.0 + + org.avaje + avaje-javaparent + 1.1 + + + org.avaje.ebeanorm + avaje-ebeanorm-spring + avaje-ebeanorm-spring + 3.1.1-SNAPSHOT + jar + Support for Spring transactions and IOC setup of Ebean server config + + + UTF-8 + 3.0.0.RELEASE + 3.1.1 + + + http://www.avaje.org + + + + LGPL version 2.1 or later + http://www.gnu.org/licenses/lgpl-2.1.txt + repo + + + + + scm:git:https://github.com/rbygrave/avaje-ebeanorm-spring.git + scm:git:https://github.com/rbygrave/avaje-ebeanorm-spring.git + https://github.com/rbygrave/avaje-ebeanorm-spring.git + + + + + javax.persistence + persistence-api + 1.0 + + + org.avaje + ebean + ${ebean.version} + + + + + org.springframework + spring-core + ${spring.framework.version} + provided + + + org.springframework + spring-aspects + ${spring.framework.version} + provided + + + org.springframework + spring-jdbc + ${spring.framework.version} + provided + + + + + org.springframework + spring-test + ${spring.framework.version} + test + + + + junit + junit + 4.8.2 + test + + + + org.apache.xbean + xbean-spring + 3.5 + test + + + + com.h2database + h2 + 1.2.128 + test + + + + org.scala-lang + scala-library + 2.10.0-M6 + provided + + + + + + + org.avaje.ebeanorm + avaje-ebeanorm-mavenenhancer + ${ebean.version} + + + main + process-test-classes + + com.avaje.ebean.** + debug=1 + + + enhance + + + + + + + diff --git a/src/main/java/META-INF/MANIFEST.MF b/src/main/java/META-INF/MANIFEST.MF new file mode 100644 index 000000000..254272e1c --- /dev/null +++ b/src/main/java/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Class-Path: + diff --git a/src/main/java/com/avaje/ebean/springsupport/factory/EbeanServerFactoryBean.java b/src/main/java/com/avaje/ebean/springsupport/factory/EbeanServerFactoryBean.java new file mode 100644 index 000000000..a1daaeb6e --- /dev/null +++ b/src/main/java/com/avaje/ebean/springsupport/factory/EbeanServerFactoryBean.java @@ -0,0 +1,85 @@ +/** + * Copyright (C) 2009 the original author or authors + * + * This file is part of Ebean. + * + * 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. + * + * 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. + * + * 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 + */ +package com.avaje.ebean.springsupport.factory; + +import org.springframework.beans.factory.FactoryBean; +import org.springframework.beans.factory.InitializingBean; + +import com.avaje.ebean.EbeanServer; +import com.avaje.ebean.EbeanServerFactory; +import com.avaje.ebean.config.ServerConfig; + +/** + * A Spring FactoryBean for constructing EbeanServer instances. + * + * @since 18.05.2009 + * @author E Mc Greal + */ +public class EbeanServerFactoryBean implements InitializingBean, FactoryBean { + + /** + * The Ebean server configuration. + */ + private ServerConfig serverConfig; + + /** + * The EbeanServer instance. + */ + private EbeanServer ebeanServer; + + public void afterPropertiesSet() throws Exception { + + 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); + } + + public EbeanServer getObject() throws Exception { + return ebeanServer; + } + + public Class getObjectType() { + return EbeanServer.class; + } + + /** + * Returns true for EbeanServer. + */ + public boolean isSingleton() { + return true; + } + + /** + * Return the server configuration. + */ + public ServerConfig getServerConfig() { + return serverConfig; + } + + /** + * Set the server configuration. + */ + public void setServerConfig(ServerConfig serverConfig) { + this.serverConfig = serverConfig; + } +} diff --git a/src/main/java/com/avaje/ebean/springsupport/txn/SpringAwareJdbcTransactionManager.java b/src/main/java/com/avaje/ebean/springsupport/txn/SpringAwareJdbcTransactionManager.java new file mode 100644 index 000000000..0fb7ac0b0 --- /dev/null +++ b/src/main/java/com/avaje/ebean/springsupport/txn/SpringAwareJdbcTransactionManager.java @@ -0,0 +1,232 @@ +/** + * Copyright (C) 2009 the original author or authors + * + * This file is part of Ebean. + * + * 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. + * + * 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. + * + * 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 + */ +package com.avaje.ebean.springsupport.txn; + +import java.util.List; +import java.util.logging.Level; +import java.util.logging.Logger; + +import javax.persistence.PersistenceException; +import javax.sql.DataSource; + +import org.springframework.jdbc.datasource.ConnectionHolder; +import org.springframework.transaction.support.TransactionSynchronization; +import org.springframework.transaction.support.TransactionSynchronizationAdapter; +import org.springframework.transaction.support.TransactionSynchronizationManager; + +import com.avaje.ebean.config.ExternalTransactionManager; +import com.avaje.ebeaninternal.api.SpiTransaction; +import com.avaje.ebeaninternal.server.transaction.DefaultTransactionThreadLocal; +import com.avaje.ebeaninternal.server.transaction.TransactionManager; + +/** + * A Spring aware TransactionScopeManager. + * + *

+ * Will look for Spring transactions and use them if they exist. + *

+ * + * @since 18.05.2009 + * @author E Mc Greal + */ +public class SpringAwareJdbcTransactionManager implements ExternalTransactionManager { + + private final static Logger logger = Logger.getLogger(SpringAwareJdbcTransactionManager.class.getName()); + + /** + * The data source. + */ + private DataSource dataSource; + + /** + * The Ebean transaction manager. + */ + private TransactionManager transactionManager; + + /** + * 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. + *

+ * Returns null if there is no current spring transaction (lazy loading outside a spring txn etc). + *

+ */ + 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. + *

+ * If it exists then we have already seen and "wrapped" this transaction. + *

+ */ + private SpringTxnListener getSpringTxnListener() { + + if (TransactionSynchronizationManager.isSynchronizationActive()){ + List 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. + *

+ * This is used by Ebean to notify it's appropriate listeners and maintain it's server + * cache etc. + *

+ */ + 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. + *

+ * When Ebean is notified (of the commit/rollback) it can then manage its + * cache, notify BeanPersistListeners etc. + *

+ */ + 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 + } + + @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); + } + } +} diff --git a/src/main/java/com/avaje/ebean/springsupport/txn/SpringJdbcTransaction.java b/src/main/java/com/avaje/ebean/springsupport/txn/SpringJdbcTransaction.java new file mode 100644 index 000000000..367e0f6bb --- /dev/null +++ b/src/main/java/com/avaje/ebean/springsupport/txn/SpringJdbcTransaction.java @@ -0,0 +1,45 @@ +/** + * Copyright (C) 2009 the original author or authors + * + * This file is part of Ebean. + * + * 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. + * + * 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. + * + * 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 + */ + +package com.avaje.ebean.springsupport.txn; + +import org.springframework.jdbc.datasource.ConnectionHolder; + +import com.avaje.ebeaninternal.server.transaction.ExternalJdbcTransaction; +import com.avaje.ebeaninternal.server.transaction.TransactionManager; + +public class SpringJdbcTransaction extends ExternalJdbcTransaction { + + private final ConnectionHolder 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(); + } + + public ConnectionHolder getConnectionHolder() { + return holder; + } +} diff --git a/src/main/resources/default-ebean-server.xml b/src/main/resources/default-ebean-server.xml new file mode 100644 index 000000000..8245aa32e --- /dev/null +++ b/src/main/resources/default-ebean-server.xml @@ -0,0 +1,29 @@ + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/test/java/com/avaje/ebean/springsupport/Account.java b/src/test/java/com/avaje/ebean/springsupport/Account.java new file mode 100644 index 000000000..d898a476b --- /dev/null +++ b/src/test/java/com/avaje/ebean/springsupport/Account.java @@ -0,0 +1,77 @@ +/** + * Copyright (C) 2009 the original author or authors + * + * This file is part of Ebean. + * + * 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. + * + * 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. + * + * 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 + */ +package com.avaje.ebean.springsupport; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.OneToOne; + +/** + * The Class Account. + * @since 18.05.2009 + * @author E Mc Greal + */ +@Entity +public class Account { + + /** The oid. */ + @Id + private long oid; + + /** The user. */ + @OneToOne + private User user; + + /** + * Gets the oid. + * + * @return the oid + */ + public long getOid() { + return oid; + } + + /** + * Sets the oid. + * + * @param oid the oid to set + */ + public void setOid(long oid) { + this.oid = oid; + } + + /** + * Gets the user. + * + * @return the user + */ + public User getUser() { + return user; + } + + /** + * Sets the user. + * + * @param user the user to set + */ + public void setUser(User user) { + this.user = user; + } +} diff --git a/src/test/java/com/avaje/ebean/springsupport/EbeanSpringModuleTest.java b/src/test/java/com/avaje/ebean/springsupport/EbeanSpringModuleTest.java new file mode 100644 index 000000000..866a02b55 --- /dev/null +++ b/src/test/java/com/avaje/ebean/springsupport/EbeanSpringModuleTest.java @@ -0,0 +1,98 @@ +/** + * Copyright (C) 2009 the original author or authors + * + * This file is part of Ebean. + * + * 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. + * + * 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. + * + * 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 + */ +package com.avaje.ebean.springsupport; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.util.logging.Logger; + +import org.junit.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests; + +/** + * Unit test for Ebean Spring Module. + * @since 18.05.2009 + * @author E Mc Greal + */ +@ContextConfiguration(locations={"/init-database.xml"}) +public class EbeanSpringModuleTest extends AbstractJUnit4SpringContextTests { + + /** The Constant logger. */ + private final static Logger logger = Logger.getLogger(EbeanSpringModuleTest.class.getName()); + + /** The user service. */ + @Autowired + private UserService userService; + + /** + * 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 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"); + } + + /** + * Gets the user service. + * + * @return the userService + */ + public UserService getUserService() { + return userService; + } + + + /** + * Sets the user service. + * + * @param userService the userService to set + */ + public void setUserService(UserService userService) { + this.userService = userService; + } +} diff --git a/src/test/java/com/avaje/ebean/springsupport/Role.java b/src/test/java/com/avaje/ebean/springsupport/Role.java new file mode 100644 index 000000000..99adbf370 --- /dev/null +++ b/src/test/java/com/avaje/ebean/springsupport/Role.java @@ -0,0 +1,73 @@ +/** + * Copyright (C) 2009 the original author or authors + * + * This file is part of Ebean. + * + * 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. + * + * 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. + * + * 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 + */ + +package com.avaje.ebean.springsupport; + +import java.util.HashSet; +import java.util.Set; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.ManyToMany; + +/** + * The Class Role. + * @since 18.05.2009 + * @author E Mc Greal + */ +@Entity +public class Role { + @Id + long oid; + + @ManyToMany + private Set users = new HashSet(); + + /** + * @return the oid + */ + public long getOid() { + return oid; + } + + /** + * @param oid the oid to set + */ + public void setOid(long oid) { + this.oid = oid; + } + + /** + * @return the users + */ + public Set getUsers() { + return users; + } + + /** + * @param users the users to set + */ + public void setUsers(Set users) { + this.users = users; + } + + + +} diff --git a/src/test/java/com/avaje/ebean/springsupport/User.java b/src/test/java/com/avaje/ebean/springsupport/User.java new file mode 100644 index 000000000..6a4a54316 --- /dev/null +++ b/src/test/java/com/avaje/ebean/springsupport/User.java @@ -0,0 +1,105 @@ +/** + * Copyright (C) 2009 the original author or authors + * + * This file is part of Ebean. + * + * 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. + * + * 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. + * + * 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 + */ + +package com.avaje.ebean.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; + +/** + * The Class User. + * @since 18.05.2009 + * @author E Mc Greal + */ +@Entity +public class User { + @Id + long oid; + + private String name; + + @ManyToMany + private Set roles = new HashSet(); + + @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 getRoles() { + return roles; + } + + /** + * @param roles the roles to set + */ + public void setRoles(Set 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; + } + +} diff --git a/src/test/java/com/avaje/ebean/springsupport/UserService.java b/src/test/java/com/avaje/ebean/springsupport/UserService.java new file mode 100644 index 000000000..7375386ae --- /dev/null +++ b/src/test/java/com/avaje/ebean/springsupport/UserService.java @@ -0,0 +1,31 @@ +/** + * Copyright (C) 2009 the original author or authors + * + * This file is part of Ebean. + * + * 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. + * + * 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. + * + * 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 + */ + +package com.avaje.ebean.springsupport; + +/** + * The Interface UserService. + * @since 18.05.2009 + * @author E Mc Greal + */ +public interface UserService { + public void save(User user); + public User find(long oid); +} diff --git a/src/test/java/com/avaje/ebean/springsupport/UserServiceImpl.java b/src/test/java/com/avaje/ebean/springsupport/UserServiceImpl.java new file mode 100644 index 000000000..65f844d72 --- /dev/null +++ b/src/test/java/com/avaje/ebean/springsupport/UserServiceImpl.java @@ -0,0 +1,74 @@ +/** + * Copyright (C) 2009 the original author or authors + * + * This file is part of Ebean. + * + * 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. + * + * 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. + * + * 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 + */ + +package com.avaje.ebean.springsupport; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.transaction.annotation.Propagation; +import org.springframework.transaction.annotation.Transactional; + +import com.avaje.ebean.EbeanServer; + +/** + * The Class UserServiceImpl. + * + * @since 18.05.2009 + * @author E Mc Greal + */ +public class UserServiceImpl implements UserService { + + /** The ebean server. */ + @Autowired + private EbeanServer ebeanServer; + + /* (non-Javadoc) + * @see org.spring.modules.ebean.UserService#save(org.spring.modules.ebean.User) + */ + @Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor=Throwable.class) + public void save(User user) { + ebeanServer.save(user); + } + + @Transactional(readOnly = true, propagation = Propagation.REQUIRED) + public User find(long oid) { + return ebeanServer.find(User.class, oid); + } + + + /** + * Gets the ebean server. + * + * @return the ebeanServer + */ + public EbeanServer getEbeanServer() { + return ebeanServer; + } + + /** + * Sets the ebean server. + * + * @param ebeanServer the ebeanServer to set + */ + public void setEbeanServer(EbeanServer ebeanServer) { + this.ebeanServer = ebeanServer; + } + + +} diff --git a/src/test/resources/init-database.xml b/src/test/resources/init-database.xml new file mode 100644 index 000000000..a91ec58c8 --- /dev/null +++ b/src/test/resources/init-database.xml @@ -0,0 +1,51 @@ + + + + + + + + + + + + + + + + + + + + + + + + com.avaje.ebean.springsupport.User + com.avaje.ebean.springsupport.Role + com.avaje.ebean.springsupport.Account + + + + + + + + + + + + + + + + + diff --git a/src/test/resources/jndi.properties b/src/test/resources/jndi.properties new file mode 100644 index 000000000..a954f4c9c --- /dev/null +++ b/src/test/resources/jndi.properties @@ -0,0 +1 @@ +java.naming.factory.initial = org.apache.xbean.spring.jndi.SpringInitialContextFactory diff --git a/src/test/resources/jndi.xml b/src/test/resources/jndi.xml new file mode 100644 index 000000000..f7af2db77 --- /dev/null +++ b/src/test/resources/jndi.xml @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file