I have a problem when I use Value object with JPA

149 Views Asked by At

I must use ValueObject in the project and JPA at the same time, but when changing the attribute to valueobject it gives me an error, I don't know yet how to solve the problem

this error: ('Id' attribute type should not be 'BrandCodigo') ('Basic' attribute type should not be 'BrandNombre' )


@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
@Entity(name = "Brand")
@Table(name = "marcas",
        uniqueConstraints = {
                @UniqueConstraint(name = "uk_marcas_nombre",columnNames = "nombre")
        })
public class Brand {
    @Id
    @Column(name = "codigo")
    private BrandCodigo codigo;
    @Column(name = "nombre",
            nullable = false,
            columnDefinition = "varchar(80)"
    )
    private BrandNombre nombre;

}

1

There are 1 best solutions below

1
On

When you refer to lombok's @Value then this is not possible afaik. Lombok's @Value is for immutable objects.

But your entity needs to be mutable since the way JPA constructs it.

Furthermore value objects do not have an identity but database entities should have primary keys.