Primary key generation problem spring-boot-jpa

Asked

Viewed 188 times

-2

I’m creating a little project using spring boot,:

org.springframework.data.relational.core.conversion.DbActionExecutionException: Failed to execute DbAction.InsertRoot(entity=br.com.voyager.central.model.Movie@640c216b, generatedId=null)

Well this is the result of the execution of :

@Test
public void insert() {

    Movie filme = new Movie("Cachorros de novo").setDescription("A historia de um vira-lata");

    repository.save(filme);// <- Erro


}

To add the most comprehensive information here to my Movie class and my super Mappedsuperclass:

  • Mappedsuperclass
@MappedSuperclass
public abstract class Entity implements Serializable{

    /**
     * 
     */
    private static final long serialVersionUID = -4231451612698516409L;

    @Id
    @GeneratedValue(generator = "uuid")
    @GenericGenerator(name = "uuid", strategy = "org.hibernate.id.UUIDGenerator")
    @Column(name = "id", unique = true)
    protected UUID uuid;

    public Entity setUUID(@NonNull UUID uuid) {
        this.uuid = uuid;
        return this;
    }

    public UUID getUUID() {
        return this.uuid;
    }

    @Override
    public boolean equals(Object object) {
        // primeiro verifica se o outro object não é nulo
        if (object != null)
            if(object instanceof Entity)// Verificar se e uma intancia de um movie
                if (this.uuid.equals(((Entity)object).getUUID())) {// verificar se é o mesmo uuid
                    return true;
                }

        return false;
    }

}
  • Movie class
@Entity
public class Movie extends br.com.voyager.central.model.Entity{

    /**
     * Serial da versãod a classe 
     */
    private static final long serialVersionUID = -7743281799376449433L;
    private String name;
    private String description;
    private String midia;

    public Movie() {}
    public Movie(@NonNull String name) {
        this.name = name;
    }
    public Movie(@NonNull UUID uuid) {
        this.uuid = uuid;
    }
    public Movie(@NonNull UUID uuid ,@NonNull String name) {
        this.uuid = uuid;
        this.name = name;
    }
    public Movie(@NonNull String uuid ,@NonNull String name) {
        this.uuid = UniqueIdentifier.getUUIDByString(uuid);
        this.name = name;
    }

    public String getDescription() {
        return description;
    }
    public Movie setDescription(@NonNull String desc) {
        this.description = desc;
        return this;
    }
    public String getMidia() {
        return midia;
    }
    public Movie setMidia(@NonNull String midia) {
        this.midia = midia;
        return this;
    }

    public Movie setName(@NonNull String name) {
        this.name = name;
        return this;
    }

    public String getName() {
        return this.name;
    }

    @Override
    public Movie setUUID(@NonNull UUID uuid) {
        super.setUUID(uuid);
        return this;
    }


}

Note that I am using UUID as the handle and the versions I am using:

  • mysql-Connector-java v5.1.48
  • spring-boot-Starter-data-jpa v2.1.9.RELEASE

And my pom is so riders:

<?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>br.com.voyager</groupId>
  <artifactId>central</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>
  <name>central</name>
  <url>http://maven.apache.org</url>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.8</java.version>
    <spring.version>2.1.9.RELEASE</spring.version>
    <hibernate.version>5.2.6.Final</hibernate.version>
  </properties>
  <dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <version>${spring.version}</version>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <version>${spring.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jdbc</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
       <groupId>mysql</groupId>
       <artifactId>mysql-connector-java</artifactId>
       <scope>runtime</scope>
       <version>5.1.48</version>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.10</version>
        <scope>provided</scope>
    </dependency>
    <!-- <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>${hibernate.version}</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>${hibernate.version}</version>
    </dependency> -->
  </dependencies>
  <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

Please help me searched everywhere for letting this automatic generation will get very annoying if I have to generate and assign one before insertion. Thanks for your help

  • The id really needs to be a UUID?

  • I sincerely do not ,but I prefer it to be so friend

  • Post the full stacktrace for kindness

  • You refer to the point where the error occurs?

1 answer

0


Mysql is a bit more boring to work with UUID, this link, this tutorial shows an alternative solution where you create a null field, and a Trigger that fills with a uuid automatically. A more elegant solution would be, in your Entity, to create a method with the @Prepersist annotation, and in this method, to generate the UUID to be saved in the database. Thus:

@PrePersist
public void prePersist() {
    this.uuid = UUID.randomUUID();
}

The annotation @PrePersist is a special Hibernate annotation that makes it possible to execute a function before any database event. In my example I put @PrePersist, but for other situations, there are notes @PreUpdate, @PostLoad, finally, several. See more here at this link.

However, it would be interesting to use another database that works better with this, suddenly Mysql version 8.0, or Postgresql, which is more robust, and with better features. I hope I’ve helped!

  • Friend I switched to version 8 of mysql and also used your annotation , but not one of them resulted the same error persists

  • I am trying to create a converter in which I solve the problem because now I am putting the UUID in hand and at the time of catching return it presents a byte error[] to uuid Imagery

  • Why from UUID to byte, not to String?

  • Stop if you have an error when I say Campu UUID should be stored as varchar

Browser other questions tagged

You are not signed in. Login or sign up in order to post.