Created
December 6, 2016 15:02
-
-
Save mp911de/7ff8557176fa1d78df1c0f00fec2d877 to your computer and use it in GitHub Desktop.
SO 40987566: How to extend CassandraRepository
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
spring.data.cassandra.keyspace-name=example |
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 com.example; | |
import org.springframework.boot.CommandLineRunner; | |
import org.springframework.boot.SpringApplication; | |
import org.springframework.boot.autoconfigure.SpringBootApplication; | |
import org.springframework.cassandra.core.WriteOptions; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.data.cassandra.repository.config.EnableCassandraRepositories; | |
@SpringBootApplication | |
@EnableCassandraRepositories(repositoryBaseClass = MyCustomizedCassandraRepository.class) | |
public class DemoApplication { | |
public static void main(String[] args) { | |
SpringApplication.run(DemoApplication.class, args); | |
} | |
@Bean | |
CommandLineRunner clr(UserRepository userRepository) { | |
return args -> { | |
userRepository.save(new User(), new WriteOptions()); | |
}; | |
} | |
} |
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 com.example; | |
import org.springframework.cassandra.core.WriteOptions; | |
import org.springframework.data.cassandra.repository.CassandraRepository; | |
import org.springframework.data.repository.NoRepositoryBean; | |
@NoRepositoryBean | |
public interface MyCassandraRepository<T> extends CassandraRepository<T> { | |
/** | |
* Customized save method. | |
*/ | |
T save(T entity, WriteOptions options); | |
} |
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 com.example; | |
import org.springframework.cassandra.core.WriteOptions; | |
import org.springframework.data.cassandra.core.CassandraOperations; | |
import org.springframework.data.cassandra.repository.MapId; | |
import org.springframework.data.cassandra.repository.query.CassandraEntityInformation; | |
import org.springframework.data.cassandra.repository.support.SimpleCassandraRepository; | |
/** | |
* Custom repository base class | |
* | |
* @param <T> | |
*/ | |
public class MyCustomizedCassandraRepository<T> extends SimpleCassandraRepository<T, MapId> | |
implements MyCassandraRepository<T> { | |
public MyCustomizedCassandraRepository(CassandraEntityInformation<T, MapId> metadata, | |
CassandraOperations operations) { | |
super(metadata, operations); | |
} | |
public T save(T entity, WriteOptions options) { | |
System.out.println(String.format("Called MyCustomizedCassandraRepository.save(%s, %s)", entity, options)); | |
return entity; | |
} | |
} |
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"?> | |
<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>com.example</groupId> | |
<artifactId>demo</artifactId> | |
<version>0.0.1-SNAPSHOT</version> | |
<packaging>jar</packaging> | |
<name>demo</name> | |
<description>Demo project for Spring Boot</description> | |
<parent> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-parent</artifactId> | |
<version>1.4.2.RELEASE</version> | |
<relativePath/> <!-- lookup parent from repository --> | |
</parent> | |
<properties> | |
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | |
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> | |
<java.version>1.8</java.version> | |
</properties> | |
<dependencies> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-data-cassandra</artifactId> | |
</dependency> | |
<dependency> | |
<groupId>org.projectlombok</groupId> | |
<artifactId>lombok</artifactId> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-test</artifactId> | |
<scope>test</scope> | |
</dependency> | |
</dependencies> | |
<build> | |
<plugins> | |
<plugin> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-maven-plugin</artifactId> | |
</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 com.example; | |
import lombok.Data; | |
import org.springframework.data.annotation.Id; | |
import org.springframework.data.cassandra.mapping.Table; | |
/** | |
* @author Mark Paluch | |
*/ | |
@Table | |
@Data | |
public class User { | |
@Id String id; | |
String firstname; | |
} |
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 com.example; | |
import java.util.UUID; | |
import org.springframework.data.cassandra.repository.Query; | |
interface UserRepository extends MyCassandraRepository<User> { | |
@Query("SELECT ...") | |
User findById(UUID id); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment