7

I need to create table PORTATION_MODEL_SET. And I need to create two keys from table portation and one key from table phone_model

enter image description here

I have code:

@Entity
@Table(name="PORTATION")
@SecondaryTable(name="PORTATION")
@Cache(usage=CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class JDBCPortation implements Portation {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Integer id;

    @ManyToOne(targetEntity=JDBCContentMetaData.class, fetch=FetchType.LAZY)
    @JoinColumn(name="fk_content_id",nullable=false)
    private JDBCContentMetaData content;


    @ManyToMany(cascade={CascadeType.ALL},fetch=FetchType.LAZY,targetEntity=JDBCPhoneModel.class)
    @JoinTable(
            name="PORTATION_MODEL_SET",
            joinColumns={
                    @JoinColumn(table="PORTATION", name="fk_portation_id", referencedColumnName="id"),
                    @JoinColumn(table="PORTATION", name="fk_content_id", referencedColumnName="fk_content_id", nullable=true)
                    },
            inverseJoinColumns=@JoinColumn(table="PHONE_MODEL",name="fk_phone_model_id", referencedColumnName="id"))
    private List<PhoneModel> phoneModel;
//more code.....
    }

and this:

@Entity
@Table(name="PHONE_MODEL")
@Cache(usage=CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class JDBCPhoneModel implements PhoneModel {

    @Id @GeneratedValue(strategy=GenerationType.AUTO)
    private Integer id;

    @Column(name="wurfl_id",length=255,nullable=false)
    private String idWurfl;

    @Column(name="name",length=100,nullable=false)
    private String modelName;
}

And this:

@Entity
@Table(name="CONTENT_META_DATA")
public class JDBCContentMetaData implements ContentMetaData {

    @Id @GeneratedValue(strategy=GenerationType.AUTO)
    private Integer id;

    @OneToMany(mappedBy="content",cascade={CascadeType.PERSIST,CascadeType.MERGE},targetEntity=JDBCPortation.class,fetch=FetchType.LAZY)
    private List<Portation> portations;
}

And I do this:

    JDBCPhoneModel phoneModel = new  JDBCPhoneModel();
    phoneModel.setIdWurfl("nokia_c50");
    phoneModel.setModelName("c50"); 

    JDBCContentMetaData content = JDBCContentMetaData.getNewInstance();
    content.getMetaData().setName("goodaaar!");
    content.getMetaData().setTitle("goood win!");
    content.setType(portationType.getContentType());
    basicRepository.saveForce(content);

    System.out.println("content_id:"+content.getId());
    JDBCPortation portation = JDBCPortation.getInstance(content);
    portation.setBinPath("/var/www/abra.jpg");
    portation.setPortitionType(portationType);
    //EXCEPTION IN SAVE FORCE
    basicRepository.saveForce(portation);

    portation.addPhoneModel(phoneModel);
    basicRepository.updateForce(portation);

but

@JoinColumn(name="fk_content_id", referencedColumnName="fk_content_id", nullable=true)

generate error:

org.springframework.transaction.TransactionSystemException: Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Error while committing the transaction
    at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:476)
    at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:754)
    at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:723)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:393)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:120)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:621)
    at ru.icb.cms.repository.db.jpa.repository.BasicRepository$$EnhancerByCGLIB$$e5fc35ad.saveForce(<generated>)
    at ru.icb.cms.repository.db.domain.RepositoryObjectTest.testCreateContent(RepositoryObjectTest.java:62)
    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:597)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)

P.S. it works for:

@ManyToMany(cascade={CascadeType.ALL},fetch=FetchType.LAZY,targetEntity=JDBCPhoneModel.class)
@JoinTable(
        name="PORTATION_MODEL_SET",
        joinColumns={
                @JoinColumn(table="PORTATION",name="fk_portation_id", referencedColumnName="id")
                },
        inverseJoinColumns=@JoinColumn(table="PHONE_MODEL",name="fk_phone_model_id", referencedColumnName="id"))
private List<PhoneModel> phoneModel;
zen0n
  • 347
  • 2
  • 6
  • 16
  • can you post the while stack trace and state when that error occurs? – yair Dec 20 '11 at 08:07
  • Can you please clean up your example a bit? Your table and column name s are all over the place (`CONTENT` or `CONTENT_META_DATA`?, `fk_phone_model_id` or `fk_model_id`?). You've also commented that you receive `org.hibernate.AnnotationException` if you use the code from my answer. Can you post *that* stack trace too? Also, I've just noticed, that `fk_content_id` in `PORTATION_MODEL_SET` is superfluous. Given the `fk_portation_id` you can get the related `fk_content_id` and its related data. Also, try changing `@SecondaryTable(name="PORTATION")` to `@SecondaryTable(name="CONTENT")`. – Kohányi Róbert Dec 20 '11 at 15:06

1 Answers1

6
@ManyToMany(targetEntity = JDBCPhoneModel.class,
            cascade = CascadeType.ALL,
            fetch = FetchType.LAZY)
@JoinTable(name = "PORTATION_MODEL_SET",
           joinColumns = {@JoinColumn(table = "PORTATION",
                                      name = "fk_portation_id", 
                                      referencedColumnName = "id"),
                          @JoinColumn(table = "PORTATION",
                                      name = "fk_content_id",                               
                                      referencedColumnName = "fk_content_id", 
                                      nullable = true)},
           inverseJoinColumns = @JoinColumn(table = "PHONE_MODEL",
                                            name="fk_phone_model_id",
                                            referencedColumnName = "id"))
private List<PhoneModel> phoneModel;

You were missing the referencedColumnName from the inverseJoinColumns definition. Please see if it's works for you.

Kohányi Róbert
  • 9,791
  • 4
  • 52
  • 81
  • new error: `Caused by: org.hibernate.AnnotationException: Cannot find the expected secondary table: no PORTATION available for ru.icb.cms.repository.db.domain.JDBCPortation` – zen0n Dec 20 '11 at 10:22
  • @zen0n Do you have a `PORTATION` named table in your database? Just because, your question shows a picture where your table's name are `Portation` instead of `PORTATION` (I've used upper-case letters, because you've used upper-case letters for `PORTATION_MODEL_SET` in your Java code snippet). For table names I always use the names defined for them in the database. If playing with names don't solve the problem (it probably won't), then please update your question and post your `PhoneModel` entity class too. Also, do you use the `@SecondaryTable` annotation anywhere in your code? – Kohányi Róbert Dec 20 '11 at 10:35