Created
April 26, 2012 16:24
-
-
Save paoloambrosio/2500752 to your computer and use it in GitHub Desktop.
Blog Post - Active Record pattern with Spring and AspectJ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Maven | |
target | |
# Eclipse IDE | |
.project | |
.classpath | |
.settings | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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" | |
xmlns:tx="http://www.springframework.org/schema/tx" | |
xsi:schemaLocation=" | |
http://www.springframework.org/schema/beans | |
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd | |
http://www.springframework.org/schema/context | |
http://www.springframework.org/schema/context/spring-context-3.0.xsd | |
http://www.springframework.org/schema/tx | |
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> | |
<context:spring-configured/> | |
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> | |
<property name="driverClassName" value="org.hsqldb.jdbcDriver" /> | |
<property name="url" value="jdbc:hsqldb:mem:mydb" /> | |
<property name="username" value="sa" /> | |
<property name="password" value="" /> | |
</bean> | |
<bean id="entityManagerFactory" | |
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> | |
<property name="dataSource" ref="dataSource" /> | |
<property name="packagesToScan" value="net.paoloambrosio.model" /> | |
<property name="jpaVendorAdapter"> | |
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> | |
<property name="showSql" value="true" /> | |
<property name="generateDdl" value="true" /> | |
<property name="databasePlatform" value="org.hibernate.dialect.HSQLDialect" /> | |
</bean> | |
</property> | |
</bean> | |
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> | |
<property name="entityManagerFactory" ref="entityManagerFactory" /> | |
</bean> | |
<tx:annotation-driven mode="aspectj" /> | |
</beans> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Copyright (c) 2012 Paolo Ambrosio | |
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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/xsd/maven-4.0.0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>net.paoloambrosio</groupId> | |
<artifactId>blog-activerecord-aspectj</artifactId> | |
<version>0.1-SNAPSHOT</version> | |
<packaging>jar</packaging> | |
<name>Active Record pattern with Spring and AspectJ</name> | |
<properties> | |
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | |
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> | |
<java.version>1.6</java.version> | |
<maven-aspectj-plugin.version>1.2</maven-aspectj-plugin.version> | |
<maven-compiler-plugin.version>2.3.2</maven-compiler-plugin.version> | |
<maven-surefire-plugin.version>2.10</maven-surefire-plugin.version> | |
<aspectj.version>1.6.12</aspectj.version> | |
<gclib.version>2.2.2</gclib.version> | |
<hibernate.version>4.0.1.Final</hibernate.version> | |
<hsqldb.version>1.8.0.10</hsqldb.version> | |
<spring.version>3.1.0.RELEASE</spring.version> | |
<junit.version>4.10</junit.version> | |
</properties> | |
<licenses> | |
<license> | |
<name>MIT License</name> | |
<url>http://www.opensource.org/licenses/mit-license.php</url> | |
</license> | |
</licenses> | |
<dependencies> | |
<dependency> | |
<groupId>org.aspectj</groupId> | |
<artifactId>aspectjrt</artifactId> | |
<version>${aspectj.version}</version> | |
</dependency> | |
<dependency> | |
<groupId>cglib</groupId> | |
<artifactId>cglib</artifactId> | |
<version>${gclib.version}</version> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework</groupId> | |
<artifactId>spring-aop</artifactId> | |
<version>${spring.version}</version> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework</groupId> | |
<artifactId>spring-aspects</artifactId> | |
<version>${spring.version}</version> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework</groupId> | |
<artifactId>spring-orm</artifactId> | |
<version>${spring.version}</version> | |
</dependency> | |
<dependency> | |
<groupId>org.hibernate</groupId> | |
<artifactId>hibernate-entitymanager</artifactId> | |
<version>${hibernate.version}</version> | |
</dependency> | |
<dependency> | |
<groupId>hsqldb</groupId> | |
<artifactId>hsqldb</artifactId> | |
<version>${hsqldb.version}</version> | |
</dependency> | |
<dependency> | |
<groupId>junit</groupId> | |
<artifactId>junit</artifactId> | |
<version>${junit.version}</version> | |
<scope>test</scope> | |
</dependency> | |
</dependencies> | |
<build> | |
<plugins> | |
<plugin> | |
<groupId>org.apache.maven.plugins</groupId> | |
<artifactId>maven-compiler-plugin</artifactId> | |
<version>${maven-compiler-plugin.version}</version> | |
<configuration> | |
<encoding>UTF-8</encoding> | |
<source>${java.version}</source> | |
<target>${java.version}</target> | |
</configuration> | |
</plugin> | |
<plugin> | |
<groupId>org.codehaus.mojo</groupId> | |
<artifactId>aspectj-maven-plugin</artifactId> | |
<version>1.2</version> | |
<!-- NB: do not use 1.3 or 1.3.x due to MASPECTJ-90 and do not use 1.4 | |
due to declare parents issue --> | |
<configuration> | |
<outxml>true</outxml> | |
<aspectLibraries> | |
<aspectLibrary> | |
<groupId>org.springframework</groupId> | |
<artifactId>spring-aspects</artifactId> | |
</aspectLibrary> | |
</aspectLibraries> | |
<complianceLevel>${java.version}</complianceLevel> | |
</configuration> | |
<dependencies> | |
<dependency> | |
<groupId>org.aspectj</groupId> | |
<artifactId>aspectjrt</artifactId> | |
<version>${aspectj.version}</version> | |
</dependency> | |
<dependency> | |
<groupId>org.aspectj</groupId> | |
<artifactId>aspectjtools</artifactId> | |
<version>${aspectj.version}</version> | |
</dependency> | |
</dependencies> | |
<executions> | |
<execution> | |
<goals> | |
<goal>compile</goal> | |
<goal>test-compile</goal> | |
</goals> | |
</execution> | |
</executions> | |
</plugin> | |
<plugin> | |
<groupId>org.apache.maven.plugins</groupId> | |
<artifactId>maven-surefire-plugin</artifactId> | |
<version>${maven-surefire-plugin.version}</version> | |
</plugin> | |
</plugins> | |
</build> | |
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.paoloambrosio.model; | |
public class User { | |
private String username; | |
public User() { | |
} | |
public User(final String username) { | |
this.username = username; | |
} | |
public String getUsername() { | |
return username; | |
} | |
public void setUsername(final String username) { | |
this.username = username; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.paoloambrosio.model; | |
import java.util.List; | |
import javax.persistence.Entity; | |
import javax.persistence.EntityManager; | |
import javax.persistence.Id; | |
import javax.persistence.PersistenceContext; | |
import org.springframework.beans.factory.annotation.Configurable; | |
import org.springframework.transaction.annotation.Transactional; | |
public aspect UserPersistence { | |
declare @type: User: @Configurable; | |
declare @type: User: @Entity; | |
declare @field: * User.username : @Id; | |
@PersistenceContext | |
private transient EntityManager User.entityManager; | |
@Transactional | |
public void User.persist() { | |
entityManager.persist(this); | |
} | |
public static List<User> User.findAll() { | |
return new User().entityManager.createQuery("SELECT o FROM User o", User.class).getResultList(); | |
} | |
public static User User.findByUsername(final String username) { | |
return new User().entityManager.find(User.class, username); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package integration.model; | |
import static org.junit.Assert.*; | |
import java.util.UUID; | |
import net.paoloambrosio.model.User; | |
import org.junit.Test; | |
import org.junit.runner.RunWith; | |
import org.springframework.test.context.ContextConfiguration; | |
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; | |
import org.springframework.transaction.annotation.Transactional; | |
@RunWith(SpringJUnit4ClassRunner.class) | |
@ContextConfiguration(locations = { "classpath:application-context.xml" }) | |
@Transactional | |
public class UserTest { | |
@Test | |
public void canBePersisted() { | |
assertEquals(0, User.findAll().size()); | |
new User(newUsername()).persist(); | |
assertEquals(1, User.findAll().size()); | |
new User(newUsername()).persist(); | |
new User(newUsername()).persist(); | |
assertEquals(3, User.findAll().size()); | |
} | |
@Test | |
public void canBeSearchedByUsername() { | |
final String username = newUsername(); | |
assertNull(User.findByUsername(username)); | |
new User(username).persist(); | |
assertNotNull(User.findByUsername(username)); | |
} | |
private String newUsername() { | |
return UUID.randomUUID().toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment