2011-12-20 257 views
4

我需要創建表PORTATION_MODEL_SET。我需要創建一個表portation兩把鑰匙,並從表PHONE_MODEL一個關鍵JPA。 JoinTable和兩個JoinColumns

enter image description here

我有代碼:

@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) 
        }, 
      [email protected](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") 
       }, 
     [email protected](table="PHONE_MODEL",name="fk_phone_model_id", referencedColumnName="id")) 
private List<PhoneModel> phoneModel; 
+0

你可以發佈時間堆棧跟蹤和狀態何時發生錯誤? – yair

+0

你可以請你清理一下你的例子嗎?你的表名和列名遍佈整個地方('CONTENT'或'CONTENT_META_DATA','fk_phone_model_id'或'fk_model_id'?)。如果您使用我的答案中的代碼,您還評論說您會收到'org.hibernate.AnnotationException'。你能發佈那個*堆棧跟蹤嗎?另外,我剛剛注意到,'PORTATION_MODEL_SET'中的'fk_content_id'是多餘的。給定'fk_portation_id',你可以得到相關的'fk_content_id'及其相關數據。另外,嘗試將'@SecondaryTable(name =「PORTATION」)'更改爲'@SecondaryTable(name =「CONTENT」)''。 –

回答

4
@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。請看看它是否適合你。

+0

新錯誤:'引起:org.hibernate.AnnotationException:無法找到預期的輔助表:沒有可用於ru.icb.cms.repository.db.domain.JDBCPortation的PORTATION – zen0n

+0

@ zen0n您是否有一個名爲「PORTATION」的名稱你的數據庫中的表?只是因爲,你的問題顯示了一張圖片,你的表名是'Portation'而不是'PORTATION'(我使用了大寫字母,因爲你在Java代碼片段中使用了'PORTATION_MODEL_SET'的大寫字母) 。對於表名,我總是使用數據庫中爲它們定義的名稱。如果使用名稱不能解決問題(可能不會),那麼請更新您的問題併發布您的'PhoneModel'實體類。另外,你是否在你的代碼的任何地方使用'@ SecondaryTable'註釋? –