Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPA. JoinTable and two JoinColumns

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;
like image 207
zen0n Avatar asked Dec 20 '11 07:12

zen0n


1 Answers

@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.

like image 170
Kohányi Róbert Avatar answered Oct 10 '22 11:10

Kohányi Róbert