简体   繁体   中英

Exception in hibernate (One To One)

I am following a basic hibernate tutorial. I am trying to make a One to One relationship between 2 tables. When I try to insert in the Client table it works, but when I try to do the opposite (insert in Facture table) I got this exception :

INFO: HHH000227: Running hbm2ddl schema export
Hibernate: alter table facture drop foreign key FK_i4kuo9pjcqstd2tpftv9eprbk
oct. 20, 2014 3:15:05 PM org.hibernate.tool.hbm2ddl.SchemaExport perform
ERROR: HHH000389: Unsuccessful: alter table facture drop foreign key FK_i4kuo9pjcqstd2tpftv9eprbk
oct. 20, 2014 3:15:05 PM org.hibernate.tool.hbm2ddl.SchemaExport perform
ERROR: Table 'mybase.facture' doesn't exist
Hibernate: drop table if exists client
Hibernate: drop table if exists facture
Hibernate: create table client (client_id integer not null auto_increment, Nom varchar(255), primary key (client_id))
Hibernate: create table facture (num_fact integer not null auto_increment, client_id integer, primary key (num_fact))
Hibernate: alter table facture add constraint FK_i4kuo9pjcqstd2tpftv9eprbk foreign key (client_id) references client (client_id)
oct. 20, 2014 3:15:05 PM org.hibernate.tool.hbm2ddl.SchemaExport execute
INFO: HHH000230: Schema export complete
Hibernate: insert into facture (client_id) values (?)
oct. 20, 2014 3:15:05 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN: SQL Error: 1452, SQLState: 23000
oct. 20, 2014 3:15:05 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: Cannot add or update a child row: a foreign key constraint fails (`mybase`.`facture`, CONSTRAINT `FK_i4kuo9pjcqstd2tpftv9eprbk` FOREIGN KEY (`client_id`) REFERENCES `client` (`client_id`))
Exception in thread "main" org.hibernate.exception.ConstraintViolationException: could not execute statement
    at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:72)
    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:126)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:112)
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:211)
    at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:96)
    at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:58)
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3032)
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3558)
    at org.hibernate.action.internal.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:98)
    at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:490)
    at org.hibernate.engine.spi.ActionQueue.addResolvedEntityInsertAction(ActionQueue.java:195)
    at org.hibernate.engine.spi.ActionQueue.addInsertAction(ActionQueue.java:179)
    at org.hibernate.engine.spi.ActionQueue.addAction(ActionQueue.java:214)
    at org.hibernate.event.internal.AbstractSaveEventListener.addInsertAction(AbstractSaveEventListener.java:324)
    at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:288)
    at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:194)
    at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:125)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:209)
    at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:55)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:194)
    at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:49)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:90)
    at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:715)
    at org.hibernate.internal.SessionImpl.save(SessionImpl.java:707)
    at org.hibernate.internal.SessionImpl.save(SessionImpl.java:702)
    at com.zack.hibernate.Main.main(Main.java:28)
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (`mybase`.`facture`, CONSTRAINT `FK_i4kuo9pjcqstd2tpftv9eprbk` FOREIGN KEY (`client_id`) REFERENCES `client` (`client_id`))
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)

This is the Client Class :

   @Entity
@Table (name="client")
public class Client implements java.io.Serializable {

    @Id /* pour dire a hibernate que id est une cle primaire*/
    @GeneratedValue /* pour que hibernate générer automatiquement la clé primaire*/

    private int client_id;

    @Column(name="Nom")/* pour forcer hibernate a nommer la colonne par "Nom"*/
    private String nom;

    @OneToOne (mappedBy = "c")
    private Facture f;

This is the Facture Class :

    @Entity
@Table(name="facture")
public class Facture implements java.io.Serializable {
    @Id
    @GeneratedValue
    private int num_fact;

    @OneToOne
    @JoinColumn(name = "client_id")
    private Client c;

And this is the Main Class:

    public class Main {
  public class Main {

    public static void main(String[] args) {
        Client c = new Client();
        c.setClient_id(1);
        c.setNom("Zack Smith");


        Facture f = new Facture();
        f.setNum_fact(1);       
        //c une étape importante
        c.setF(f);

        f.setC(c);



        SessionFactory s = new AnnotationConfiguration().configure().buildSessionFactory();
        Session ss = s.openSession();
        ss.beginTransaction();

        ss.save(f);

        ss.getTransaction().commit();
        ss.close();
        s.close();

    }
}

Picture of the database : http://hpics.li/f1a3ee7

It looks to me like you might be slightly confused as to how Hibernate actually sets up these relationships. I think your exception is being caused you are saying that your clientId column/field is a generated value mapped to the Facture table, when you actually want it to refer to the clientId column/field on the Client table as a foreign key. So you are trying to save your Facture object, which causes the Facture.clientId column to be generated (because you annotated it with a @GenericGenerator ). Because this value is set, Hibernate tried to update an existing Client object with that generated ID. Because it doesn't actually exist, you get the exception that you posted.

What you are attempting to do is essentially allow Hibernate to map between a foreign key relationship so that you can get Client from the Facture and vice versa.

You don't need to specify a join column, Hibernate defaults to the @Id column for all relationships. You should modify your Client class as follows

@Entity
@Table (name="client")
public class Client implements java.io.Serializable {

    @Id 
    @GeneratedValue 
    private int clientId;

    //Keep the nom field, I just removed it for brevity.

    @OneToOne (cascade = CascadeType.ALL)
    private Facture facture;

and modify your Facture class as follows:

@Entity
@Table(name="facture")
public class Facture implements java.io.Serializable {

    @Id 
    @GeneratedValue
    private int factureId;

    @OneToOne (cascade = CascadeType.ALL)
    private Client client;

Assuming that your database is set up correctly, this should be all you need for Hibernate to correctly save your objects when you call save on either of them.

Try this, and if you can't get it to work, let me know.

public class Main {

    public static void main(String[] args) {

        Client c = new Client();
        c.setId(1);;
        c.setNom("Zack");


        Facture f = new Facture();
        f.setNum_fact(1);
        f.setC(c);
        c.setF(f);


//        Facture f = new Facture();
//        f.setNum_fact(1);       
//        //c une étape importante
//        c.setF(f);
//
//        f.setC(c);



        SessionFactory s = new AnnotationConfiguration().configure().buildSessionFactory();
        Session ss = s.openSession();
        ss.beginTransaction();
        ss.save(c);
        ss.save(f);

        ss.getTransaction().commit();
        ss.close();
        s.close();


    }

}

===============================================================================================

oct. 22, 2014 6:50:34 PM org.hibernate.tool.hbm2ddl.SchemaExport execute
INFO: HHH000227: Running hbm2ddl schema export
Hibernate: alter table Facture drop foreign key FK_rqdue6x5d4b9lg97rxve2fq20
oct. 22, 2014 6:50:34 PM org.hibernate.tool.hbm2ddl.SchemaExport perform
ERROR: HHH000389: Unsuccessful: alter table Facture drop foreign key FK_rqdue6x5d4b9lg97rxve2fq20
oct. 22, 2014 6:50:34 PM org.hibernate.tool.hbm2ddl.SchemaExport perform
ERROR: Table 'mybase.facture' doesn't exist
Hibernate: drop table if exists Client
Hibernate: drop table if exists Facture
Hibernate: create table Client (id integer not null auto_increment, nom varchar(255), primary key (id))
Hibernate: create table Facture (num_fact integer not null auto_increment, client_id integer, primary key (num_fact))
Hibernate: alter table Facture add constraint FK_rqdue6x5d4b9lg97rxve2fq20 foreign key (client_id) references Client (id)
oct. 22, 2014 6:50:34 PM org.hibernate.tool.hbm2ddl.SchemaExport execute
INFO: HHH000230: Schema export complete
Hibernate: insert into Client (nom) values (?)
Hibernate: insert into Facture (client_id) values (?)
oct. 22, 2014 6:50:34 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl stop
INFO: HHH000030: Cleaning up connection pool [jdbc:mysql://localhost:3306/mybase]

Thank you guys for your help, it works :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM