#11 - Repackage to io.ebean.springtxn ... remove factory bean support (move it to src/test as example)

This commit is contained in:
Rob Bygrave
2016-12-13 21:50:11 +13:00
parent 326fc221bf
commit 2f793c29dc
15 changed files with 64 additions and 172 deletions
+57
View File
@@ -0,0 +1,57 @@
/**
* 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
*/
package org.example;
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 id. */
@Id
private Long id;
/** The user. */
@OneToOne
private User user;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
@@ -0,0 +1,85 @@
/**
* 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
*/
package org.example;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import io.ebean.EbeanServer;
import io.ebean.EbeanServerFactory;
import io.ebean.config.ServerConfig;
/**
* A Spring FactoryBean for constructing EbeanServer instances.
*
* @since 18.05.2009
* @author E Mc Greal
*/
public class EbeanServerFactoryBean implements InitializingBean, FactoryBean<EbeanServer> {
/**
* 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<? extends EbeanServer> 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;
}
}
@@ -0,0 +1,100 @@
/**
* 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
*/
package org.example;
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.SpringJUnit4ClassRunner;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
/**
* Unit test for Ebean Spring Module.
* @since 18.05.2009
* @author E Mc Greal
*/
@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 user service. */
@Autowired
private UserService userService;
/**
* Create the test case.
*/
public EbeanSpringModuleTest() {
super();
}
@Test
public void testBatchInsert() {
userService.batchInsert();
}
/**
* 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");
}
/**
* Return the user service.
*/
public UserService getUserService() {
return userService;
}
/**
* Sets the user service.
*/
public void setUserService(UserService userService) {
this.userService = userService;
}
}
+57
View File
@@ -0,0 +1,57 @@
/**
* 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
*/
package org.example;
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 id;
@ManyToMany
private Set<User> users;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Set<User> getUsers() {
return users;
}
public void setUsers(Set<User> users) {
this.users = users;
}
}
+78
View File
@@ -0,0 +1,78 @@
/**
* 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 org.example;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.OneToOne;
import java.util.Set;
/**
* The Class User.
* @since 18.05.2009
* @author E Mc Greal
*/
@Entity
public class User {
@Id
long oid;
private String name;
@ManyToMany
private Set<Role> roles;
@OneToOne(mappedBy="user")
private Account account;
public long getOid() {
return oid;
}
public void setOid(long oid) {
this.oid = oid;
}
public Set<Role> getRoles() {
return roles;
}
public void setRoles(Set<Role> roles) {
this.roles = roles;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Account getAccount() {
return account;
}
public void setAccount(Account account) {
this.account = account;
}
}
@@ -0,0 +1,35 @@
/**
* 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 org.example;
/**
* The Interface UserService.
* @since 18.05.2009
* @author E Mc Greal
*/
public interface UserService {
void save(User user);
User find(long id);
void batchInsert();
}
@@ -0,0 +1,97 @@
/**
* 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 org.example;
import io.ebean.Transaction;
import io.ebean.PersistBatch;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import io.ebean.EbeanServer;
import java.util.ArrayList;
import java.util.List;
/**
* 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 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-------");
}
/**
* Return the ebean server.
*/
public EbeanServer getEbeanServer() {
return ebeanServer;
}
/**
* Sets the ebean server.
*/
public void setEbeanServer(EbeanServer ebeanServer) {
this.ebeanServer = ebeanServer;
}
}