MySQL Autoinc FK [message #389702] |
Mon, 22 June 2009 07:38 |
Devinder Singh Messages: 8 Registered: July 2009 |
Junior Member |
|
|
I have two entities :
Pessoa :
// Data Defination
CREATE TABLE `pessoa` (
`ID_PESSOA` int(11) NOT NULL AUTO_INCREMENT,
`NO_PESSOA` varchar(80) NOT NULL,
PRIMARY KEY (`ID_PESSOA`)
) ENGINE=InnoDB ;
@Entity
@Table(name = "pessoa")
public class Pessoa implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "ID_PESSOA")
private Integer idPessoa;
@Basic(optional = false)
@Column(name = "NO_PESSOA")
private String noPessoa;
@OneToOne(cascade = CascadeType.ALL, mappedBy = "pessoa", fetch =
FetchType.LAZY)
private PessoaFisica pessoaFisica;
PessoaFisica :
// Data Defination
CREATE TABLE `pessoa_fisica` (
`ID_PESSOA` int(11) NOT NULL,
`NR_CPF` char(11) COLLATE utf8_unicode_ci DEFAULT NULL,
`id_ESTADO_CIVIL` smallint(6) DEFAULT NULL,
`dt_nascimento` date DEFAULT NULL,
PRIMARY KEY (`ID_PESSOA`),
KEY `NR_CPF` (`NR_CPF`),
KEY `fk_pessoa_fisica_pessoa` (`ID_PESSOA`),
CONSTRAINT `fk_pessoa_fisica_pessoa` FOREIGN KEY (`ID_PESSOA`)
REFERENCES `pessoa` (`ID_PESSOA`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB ;
@Entity
@Table(name = "pessoa_fisica")
public class PessoaFisica implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@Column(name = "ID_PESSOA")
private Integer idPessoa;
@Column(name = "NR_CPF")
private String nrCpf;
@Column(name = "id_ESTADO_CIVIL")
private Short idESTADOCIVIL;
@Column(name = "dt_nascimento")
@Temporal(TemporalType.DATE)
private Date dtNascimento;
@JoinColumn(name = "ID_PESSOA", referencedColumnName = "ID_PESSOA",
insertable = false, updatable = false)
@OneToOne(optional = false, fetch = FetchType.LAZY)
private Pessoa pessoa;
While persisting Pessoa I am getting following error :
SEVERE: >>javax.persistence.RollbackException: Exception
[EclipseLink-4002] (Eclipse Persistence Services - 1.1.0.r3634):
org.eclipse.persistence.exceptions.DatabaseException
Internal Exception:
com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationE xception:
Column 'ID_PESSOA' cannot be null
Error Code: 1048
Call: INSERT IGNORE INTO pessoa_fisica (ID_PESSOA, id_ESTADO_CIVIL,
dt_nascimento, NR_CPF) VALUES (?, ?, ?, ?)
bind => [null, 1, 2009-06-16, 001.797.717-79]
Query:
InsertObjectQuery(br.com.mecsoft.imobiliario.model.PessoaFis ica[idPessoa=null])
>>org.eclipse.persistence.exceptions.DatabaseException:
Internal Exception:
com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationE xception:
Column 'ID_PESSOA' cannot be null
Error Code: 1048
Call: INSERT IGNORE INTO pessoa_fisica (ID_PESSOA, id_ESTADO_CIVIL,
dt_nascimento, NR_CPF) VALUES (?, ?, ?, ?)
bind => [null, 1, 2009-06-16, 001.797.717-79]
Query:
InsertObjectQuery(br.com.mecsoft.imobiliario.model.PessoaFis ica[idPessoa=null])
[SQL: 1048, 23000]
>> com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationE xception: Column
'ID_PESSOA' cannot be null
>> at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:931 )
>> at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2870)
>> at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1573)
>> at
com.mysql.jdbc.ServerPreparedStatement.serverExecute(ServerP reparedStatement.java:1169)
>> at
com.mysql.jdbc.ServerPreparedStatement.executeInternal(Serve rPreparedStatement.java:693)
>>...
|
|
|
Re: MySQL Autoinc FK [message #389703 is a reply to message #389702] |
Mon, 22 June 2009 07:59 |
Tom Eugelink Messages: 825 Registered: July 2009 |
Senior Member |
|
|
> CREATE TABLE `pessoa_fisica` (
> `ID_PESSOA` int(11) NOT NULL,
> @Id
> @Basic(optional = false)
> @Column(name = "ID_PESSOA")
> While persisting Pessoa I am getting following error :
>
> SEVERE: >>javax.persistence.RollbackException: Exception
> [EclipseLink-4002] (Eclipse Persistence Services - 1.1.0.r3634):
> org.eclipse.persistence.exceptions.DatabaseException
> Internal Exception:
> com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationE xception:
> Column 'ID_PESSOA' cannot be null
> Error Code: 1048
> Call: INSERT IGNORE INTO pessoa_fisica (ID_PESSOA, id_ESTADO_CIVIL,
> dt_nascimento, NR_CPF) VALUES (?, ?, ?, ?)
> bind => [null, 1, 2009-06-16, 001.797.717-79]
> Query:
> 'ID_PESSOA' cannot be null
AFAIK you have defined pessoa_fisica.ID_PESSOA as "not null", but have not defined an AUTO_INCREMENT on it in the table, nor an @GeneratedValue on the Class. So it is never assigned a value.
Tom
|
|
|
|
|
|
|
|
|
|
|
|
Re: MySQL Autoinc FK [message #389717 is a reply to message #389706] |
Mon, 22 June 2009 19:53 |
|
No it will not. You must assign the value to your idPessoa field
yourself. If it is derived from the 1-1 pessoa, then in your setPessoa()
you should also set the idPessoa.
If you make the 1-1 writable and the Id insert/updatable=false, then it
will write from the 1-1, but you should still keep the basic in synch in
your set method.
In JPA 2.0, you can avoid the basic id and just have the 1-1 and mark it
as the Id.
---
James
http://www.nabble.com/EclipseLink---Users-f26658.html
James : Wiki : Book : Blog : Twitter
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.29224 seconds