Skip to content

Commit

Permalink
Use @UuidGenerator for JPA
Browse files Browse the repository at this point in the history
  • Loading branch information
trisberg committed Dec 11, 2024
1 parent ab62fea commit 860f351
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 20 deletions.
1 change: 1 addition & 0 deletions spring-music/accelerator.axl
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ engine {
if (#persistenceType == 'jpa') {
select(
!"src/main/java/com/example/music/config/data/**" &&
!"src/main/java/com/example/music/domain/RandomIdGenerator.java" &&
!"src/main/java/com/example/music/repositories/mongodb/**" &&
!"src/main/java/com/example/music/repositories/redis/**")
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
package com.example.music.domain;

// #IF(#persistenceType == 'jpa')
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.UuidGenerator;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;

@Entity
// #ENDIF
public class Album {
// #IF(#persistenceType == 'jpa')
@Id
@UuidGenerator
@Column(length=40)
@GeneratedValue(generator="randomId")
@GenericGenerator(name="randomId", strategy="com.example.music.domain.RandomIdGenerator")
// #ENDIF
private String id;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,8 @@
package com.example.music.domain;

// #IF(#persistenceType == 'jpa')
import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.id.IdentifierGenerator;

import java.io.Serializable;
// #ENDIF
import java.util.UUID;

// #IF(#persistenceType == 'jpa')
public class RandomIdGenerator implements IdentifierGenerator {
@Override
public Serializable generate(SharedSessionContractImplementor session, Object object) throws HibernateException {
return generateId();
}

// #ELSE
public class RandomIdGenerator {
// #ENDIF
public String generateId() {
return UUID.randomUUID().toString();
}
Expand Down

0 comments on commit 860f351

Please sign in to comment.