hibernate annotation one-to-many
От: -Sergey- Украина  
Дата: 12.03.08 13:12
Оценка:
привет всем.
проблема в следующем: создается новый объект, у которого есть отношение один ко многим к другому классу, в данный Лист добавляются новые экземпляры при сохранении базового класса происходит ексепшин, ругается на отсуствие foreignkey — нов базеон есть, скорее всего напутал где-то с аннотациями но самому найти не удалось ..

@Entity
@Table( name = "Bill" )
public class Bill
{
    private long billId;
    private Set<Payment> payments = new HashSet<Payment>();

    public Bill()
    {
    }

    @Id
    @GeneratedValue( strategy = GenerationType.AUTO )
    public long getBillId()
    {
        return billId;
    }

    @OneToMany( fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "paymentId")
    public Set<Payment> getPayments()
    {
        return payments;
    }
}


@Entity
@Table( name = "Payment" )
public class Payment
{
    private long paymentId;
    private String name;
    private Bill bill;


    public Payment()
    {
    }


    @Id
    @GeneratedValue( strategy = GenerationType.AUTO )
    public long getPaymentId()
    {
        return paymentId;
    }

    @ManyToOne( fetch = FetchType.LAZY )
    @JoinColumn( name = "billId" )
    public Bill getBill()
    {
        return bill;
    }

}


эксепшен

12.3.2008 03:00:51 - [logger: org.hibernate.tool.hbm2ddl.SchemaExport ] INFO:  Running hbm2ddl schema export
12.3.2008 03:00:51 - [logger: org.hibernate.tool.hbm2ddl.SchemaExport ] INFO:  exporting generated schema to database
12.3.2008 03:00:51 - [logger: org.hibernate.tool.hbm2ddl.SchemaExport ] INFO:  schema export complete
Hibernate: select gen_id( hibernate_sequence, 1 ) from RDB$DATABASE
Hibernate: select gen_id( hibernate_sequence, 1 ) from RDB$DATABASE
Hibernate: select gen_id( hibernate_sequence, 1 ) from RDB$DATABASE
Hibernate: insert into Bill (billId) values (?)
Hibernate: insert into Payment (name, billId, paymentId) values (?, ?, ?)
12.3.2008 03:00:52 - [logger: org.hibernate.util.JDBCExceptionReporter ] WARN:  SQL Error: 335544466, SQLState: HY000
12.3.2008 03:00:52 - [logger: org.hibernate.util.JDBCExceptionReporter ] ERROR:  GDS Exception. 335544466. violation of FOREIGN KEY constraint "FK3454C9E6497E4391" on table "PAYMENT"
Foreign key reference target does not exist
12.3.2008 03:00:52 - [logger: org.hibernate.event.def.AbstractFlushingEventListener ] ERROR:  Could not synchronize database state with session
org.hibernate.exception.GenericJDBCException: could not insert: [com.common.model.sub.Payment]
    at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:103)
    at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:91)
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2263)
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2656)
    at org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:52)
    at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:248)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:232)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:139)
    at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
    at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
    at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
    at org.springframework.orm.hibernate3.HibernateAccessor.flushIfNecessary(HibernateAccessor.java:388)
    at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:363)
    at org.springframework.orm.hibernate3.HibernateTemplate.saveOrUpdate(HibernateTemplate.java:676)
    at com.common.dao.impl.BillDaoImpl.saveBill(BillDaoImpl.java:17)
    at com.common.run.MainUnit.main(MainUnit.java:80)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:585)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:90)
Caused by: org.firebirdsql.jdbc.FBSQLException: GDS Exception. 335544466. violation of FOREIGN KEY constraint "FK3454C9E6497E4391" on table "PAYMENT"
Foreign key reference target does not exist
    at org.firebirdsql.jdbc.AbstractPreparedStatement.internalExecute(AbstractPreparedStatement.java:711)
    at org.firebirdsql.jdbc.AbstractPreparedStatement.executeUpdate(AbstractPreparedStatement.java:171)
    at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:23)
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2243)
    ... 18 more


сам код который вызывает исключение


       Bill bill = new Bill();

        Payment payments = new Payment();
        payments.setName( "fruits" );

        Payment payment2 = new Payment();
        payment2.setName( "goods" );

        bill.getPayments().add( payment2 );
        bill.getPayments().add( payments );

        IBillDAO billDAO = ( IBillDAO ) applicationContext.getBean( "billDao" );
        billDAO.saveBill( bill );
-
[color="gray"]
Цель в жизни определяет все..
Выбор есть всегда..
Но мы часто не хотим его делать..
[/color]
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.