1

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;

}

  • What is the error you're getting? – stdunbar Apr 26 '21 at 15:02
  • 1
    'Id' attribute type should not be 'BrandCodigo' – KurisuWIzard Apr 26 '21 at 15:04
  • What sort of table structure do you want for this object model? is the Brand going to have references to a BrandNombre and BrandCodigo tables - or are these just meant to be single data fields within the Brand table. If the later, you need to specify how JPA is going to pull out the relevant data from your value objects; tell it what is relevant and what isn't by making it an embeddable. If they are tables, you need to set them up as Entities and use OneToOne and ManyToOne reference mappings as appropriate. – Chris Apr 26 '21 at 19:23
  • Those two are non-table attributes, only that attributes are ValueObjects, and I want the JPA to read me as ValueObject as an attribute and I do not generate an error – KurisuWIzard Apr 26 '21 at 19:45

1 Answers1

0

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.

Fabian Barney
  • 14,219
  • 5
  • 40
  • 60