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
/** | |
* 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")) |
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
@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 |
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
@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 |
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
public interface UserRepository extends CrudRepository<User, Long> {} |
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
public interface GroupRepository extends CrudRepository<Group, Long> {} |
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
{ | |
"_links" : { | |
"groups" : { | |
"href" : "http://localhost:8080/groups" | |
}, | |
"users" : { | |
"href" : "http://localhost:8080/users" | |
}, | |
"profile" : { | |
"href" : "http://localhost:8080/profile" |
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
{ | |
"name": "A", | |
"_links": { | |
"self": { | |
"href": "http://localhost:8080/groups/1" | |
}, | |
"group": { | |
"href": "http://localhost:8080/groups/1" | |
}, | |
"users": { |
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
/** | |
* Only defines a {@link com.samkruglov.entities.User#username username}. | |
*/ | |
public class UserUsername { | |
private final String username; // getter & consctuctor | |
// equals & hashcode | |
} |
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
public interface UserRepository extends CrudRepository<User, Long> { | |
@RestResource(path = "username", rel = "getUsername") | |
UserUsername findUsernameById(@Param("id") Long id); | |
} |
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
{ | |
"username": "jsmith" | |
} |
OlderNewer