Reputation: 557
I'm a little new to JPA and I'm trying to get my entity classes to work properly, but my code is always returning me this error: "Multiple writable mappings exist for the field [PRODUTOS.MARCA_IDMARCA]"
These are my entity classes:
@Entity
public class Marca implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer idMarca;
private String nome;
private Integer idFornecedores;
.
.
.
}
I'm trying to make an unidirectional ManyToOne relationship between produtos and Marca (1 Marca to Many produtos):
@Entity
public class produtos implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long idProdutos;
private int codBarras;
private String nome;
private float preco;
private int qtdProduto;
private int idCategoriaProduto;
@ManyToOne
private CategoriaProduto categoria;
private int Marca_idMarca;
@ManyToOne
private Marca marca;
.
.
.
}
Error:
Exception Description: Multiple writable mappings exist for the field [PRODUTOS.MARCA_IDMARCA]. Only one may be defined as writable, all others must be specified read-only.
Mapping: org.eclipse.persistence.mappings.ManyToOneMapping[marca]
Descriptor: RelationalDescriptor(ClassesDeEntidade.produtos --> [DatabaseTable(PRODUTOS)])
I really don't know what's happening. Can anyone explain to me why this code is wrong?
EDIT:
Table definitions:
Produtos:
CREATE TABLE IF NOT EXISTS mydb
.Produtos
(
idProdutos
INT NOT NULL AUTO_INCREMENT,
codBarras
INT(13) NOT NULL,
nome
VARCHAR(150) NOT NULL,
preco
FLOAT NOT NULL,
qtdProduto
INT NOT NULL,
idCategoriaProduto
INT NOT NULL,
Marca_idMarca
INT NOT NULL,
PRIMARY KEY (idProdutos
),
INDEX fk_Produtos_CategoriaProduto1_idx
(idCategoriaProduto
ASC),
INDEX fk_Produtos_Marca1_idx
(Marca_idMarca
ASC),
CONSTRAINT fk_Produtos_CategoriaProduto1
FOREIGN KEY (idCategoriaProduto
)
REFERENCES mydb
.CategoriaProduto
(idCategoriaProduto
)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT fk_Produtos_Marca1
FOREIGN KEY (Marca_idMarca
)
REFERENCES mydb
.Marca
(idMarca
)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
Marca:
CREATE TABLE IF NOT EXISTS mydb
.Marca
(
idMarca
INT NOT NULL AUTO_INCREMENT,
nome
VARCHAR(45) NOT NULL,
idFornecedores
INT NOT NULL,
PRIMARY KEY (idMarca
),
INDEX fk_Marca_Fornecedores1_idx
(idFornecedores
ASC),
CONSTRAINT fk_Marca_Fornecedores1
FOREIGN KEY (idFornecedores
)
REFERENCES mydb
.Fornecedores
(idFornecedores
)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
Upvotes: 0
Views: 143
Reputation: 367
Can you show your table definitions?
The problem seems to be that you have two fields (probably Marca marca
, which have the @ManyToOne
annotation and int Marca_idMarca
, that has the same name) mapping to the same PRODUTO.ID_MARCA
column in the database.
I know I should post this as a comment, but I'm not able to comment yet.
Upvotes: 0