Skip to content

Instantly share code, notes, and snippets.

@Sam-Kruglov
Sam-Kruglov / package-info.java
Created January 9, 2018 11:31
Create PostgreSQL sequence generator
/**
* Defines common Id Generator that use PostgreSQL sequences properly.
* It uses a separate sequence for the chosen ID, as opposed to using a single sequence for all IDs;
* the same result can be achieved by simply using {@code @GeneratedValue(strategy = GenerationType.IDENTITY)}, but
* that way hibernate would use a sequence as an identity, which would decrease performance.
* <p>
* Generates a sequence for each entity "entityName_seq"
*/
@GenericGenerator(name = "SequencePerEntityGenerator", strategy = "org.hibernate.id.enhanced.SequenceStyleGenerator",
parameters = @Parameter(name = "prefer_sequence_per_entity", value = "true"))
@Sam-Kruglov
Sam-Kruglov / User.java
Created January 9, 2018 11:40
User entity
@Entity
@Table(name = "users")
public class User implements Identifiable<Long> {
@Id
@GeneratedValue(generator = "SequencePerEntityGenerator")
private Long id; // only getter
@Column(length = 16, nullable = false, unique = true)
private String username; // getter and setter
@Sam-Kruglov
Sam-Kruglov / Group.java
Created January 9, 2018 11:43
Group entity
@Entity
@Table(name = "groups")
public class Group implements Identifiable<Long> {
@Id
@GeneratedValue(generator = "SequencePerEntityGenerator")
private Long id; // getter
@Column(nullable = false, unique = true)
private String name; // getter and setter
@Sam-Kruglov
Sam-Kruglov / UserRepository.java
Created January 9, 2018 11:44
User repository
public interface UserRepository extends CrudRepository<User, Long> {}
@Sam-Kruglov
Sam-Kruglov / GroupRepository.java
Created January 9, 2018 11:45
Group repository
public interface GroupRepository extends CrudRepository<Group, Long> {}
@Sam-Kruglov
Sam-Kruglov / REST_response.json
Last active January 11, 2018 04:13
Initial Response for GET localhost:8080
{
"_links" : {
"groups" : {
"href" : "http://localhost:8080/groups"
},
"users" : {
"href" : "http://localhost:8080/users"
},
"profile" : {
"href" : "http://localhost:8080/profile"
@Sam-Kruglov
Sam-Kruglov / REST_response.json
Last active January 11, 2018 04:13
Initial Response for GET localhost:8080/groups/1
{
"name": "A",
"_links": {
"self": {
"href": "http://localhost:8080/groups/1"
},
"group": {
"href": "http://localhost:8080/groups/1"
},
"users": {
@Sam-Kruglov
Sam-Kruglov / UserUsername.java
Last active January 11, 2018 10:26
DTO for User
/**
* Only defines a {@link com.samkruglov.entities.User#username username}.
*/
public class UserUsername {
private final String username; // getter & consctuctor
// equals & hashcode
}
@Sam-Kruglov
Sam-Kruglov / UserRepository.java
Last active January 11, 2018 10:13
Integrate the DTO to Spring Data
public interface UserRepository extends CrudRepository<User, Long> {
@RestResource(path = "username", rel = "getUsername")
UserUsername findUsernameById(@Param("id") Long id);
}
@Sam-Kruglov
Sam-Kruglov / REST_response.json
Last active January 11, 2018 05:12
DTO GET localhost:8080/users/search/username?id=1
{
"username": "jsmith"
}