编程语言
首页 > 编程语言> > java – JPA. JoinTable和两个JoinColumns

java – JPA. JoinTable和两个JoinColumns

作者:互联网

我需要创建表PORTATION_MODEL_SET.我需要从表portation创建两个键,从table phone_model创建一个键

我有代码:

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

还有这个:

@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;
}

还有这个:

@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;
}

我这样做:

    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);

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

生成错误:

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)

附:它适用于:

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

解决方法:

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

您缺少来自inverseJoinColumns定义的referencedColumnName.请查看它是否适合您.

标签:java,jpa,hibernate,jointable
来源: https://codeday.me/bug/20190521/1146935.html