Wątek przeniesiony 2023-11-29 20:16 z Bazy danych przez Riddle.

Jak wygenerować UUID dla klucza głównego?

0

Cześć. Prośba o looka:

@Entity
@Table(name = "QUESTION_DEFINITION")
data class QuestionDefinition(
    @Id
    @Column(nullable = false, length = 36)
    @GeneratedValue(generator = "uuid")
    @GenericGenerator(name = "uuid", strategy = "uuid2")
    val id: String?,

    @Column(nullable = false, length = 1024)
    val question: String,

) : Serializable

liquibase:

```
<?xml version="1.1" encoding="UTF-8" standalone="no"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
                   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                   xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.6.xsd">
    <changeSet author="process" id="2">
        <createTable tableName="QUESTION_DEFINITION">
            <column computed="false" name="id" type="varchar(36)">
                <constraints nullable="false" primaryKey="true" primaryKeyName="PK_213E83FE70C9317"/>
            </column>
            <column name="question" type="varchar(1024)">
                <constraints nullable="false" />
            </column>
        </createTable>
    </changeSet>
    <changeSet author="process" id="6">
        <insert tableName="QUESTION_DEFINITION">
            <column name="question" value="fox"/>
        </insert>
    </changeSet>
</databaseChangeLog>
```

application.properties:

```
spring.datasource.url=jdbc:postgresql://localhost:5432/aa
spring.datasource.username=postgres
spring.datasource.password=postgres
# Konfiguracja Hibernate
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.ddl-auto=update
spring.datasource.driver-class-name=org.postgresql.Driver
spring.liquibase.change-log=classpath:/liquibase/*.xml
```

Dostaję:
Caused by: org.postgresql.util.PSQLException: ERROR: null value in column "id" of relation "question_definition" violates not-null constraint
Detail: Failing row contains (null, fox).

Co robię źle? Dzięki!

0

Jeszcze pom.xml:

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>3.2.0</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.example</groupId>
	<artifactId>demo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>demo</name>
	<description>Demo project for Spring Boot</description>
	<properties>
		<java.version>17</java.version>
		<kotlin.version>1.9.20</kotlin.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-jpa</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
		<dependency>
			<groupId>com.fasterxml.jackson.module</groupId>
			<artifactId>jackson-module-kotlin</artifactId>
		</dependency>
		<dependency>
			<groupId>org.jetbrains.kotlin</groupId>
			<artifactId>kotlin-reflect</artifactId>
		</dependency>
		<dependency>
			<groupId>org.jetbrains.kotlin</groupId>
			<artifactId>kotlin-stdlib</artifactId>
		</dependency>
		<dependency>
			<groupId>org.liquibase</groupId>
			<artifactId>liquibase-core</artifactId>
		</dependency>

		<dependency>
			<groupId>org.postgresql</groupId>
			<artifactId>postgresql</artifactId>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>RELEASE</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>

	<build>
		<sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
		<testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
			<plugin>
				<groupId>org.jetbrains.kotlin</groupId>
				<artifactId>kotlin-maven-plugin</artifactId>
				<configuration>
					<args>
						<arg>-Xjsr305=strict</arg>
					</args>
					<compilerPlugins>
						<plugin>spring</plugin>
						<plugin>jpa</plugin>
					</compilerPlugins>
				</configuration>
				<dependencies>
					<dependency>
						<groupId>org.jetbrains.kotlin</groupId>
						<artifactId>kotlin-maven-allopen</artifactId>
						<version>${kotlin.version}</version>
					</dependency>
					<dependency>
						<groupId>org.jetbrains.kotlin</groupId>
						<artifactId>kotlin-maven-noarg</artifactId>
						<version>${kotlin.version}</version>
					</dependency>
				</dependencies>
			</plugin>
		</plugins>
	</build>

</project>
0

mam takie pytanie - może niezasadne - czemu chcesz używać UUID jako "id" w tabeli?

a próbowałeś usunąć znak zapytania stąd => val id: String?,
może JPA się domaga wartości, a niechcąco podajesz Optionala?

0

Powinieneś zrezygnować z UUID i sekwencji

@SequenceGenerator(name="pk_sequence",sequenceName="entity_id_seq")
@GeneratedValue(strategy=GenerationType.SEQUENCE,generator="pk_sequence")

0

@Babel2024: z tej odpowiedzi co sam podesłałeś:

Note that @Column(length = 36) is important to reduce from 255 to 36 the field length in MySQL.

Note that with PostgreSQL you should use @Type(type = "pg-uuid") instead.

import org.hibernate.annotations.Type
import java.util.UUID
import javax.persistence.Column
import javax.persistence.GeneratedValue
import javax.persistence.Id

@Id @GeneratedValue
@Type(type = "uuid-char") @Column(length = 36)
private UUID id;

Próbowaleś pg-uuid?

0

mi się nie chce zgadywać... w miarę możliwości podaj link do repo, będzie łatwiej

0

Bez sensu robić to w hibernate jak można w samym postgresql ...

CREATE EXTENSION pgcrypto;
CREATE TABLE tabelka(
   id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
   ...
);
0

Dokładnie, Woolfik. Właśnie dopisałem w yamlu (i poszło):

  • column:
    name: id
    valueComputed: 'NEWID()'
    Dzięki!

1 użytkowników online, w tym zalogowanych: 0, gości: 1