2017-12-16 5 views
2

に存在する場合、支店およびエンタープライズ、2つのエンティティがAbstractEntityJPA - 親は私は2つのエンティティを持っているManyToOne関係

AbstractEntity

@Id 
@Column(length=36) 
@GeneratedValue(generator = "uuid") 
@GenericGenerator(name = "uuid", strategy = "uuid2") 
private String id; 

支店エンティティの拡張要素を保存します。

private String name; 
private int code; 
@ManyToOne 
@JoinColumn(name = "enterprise_id") 
private Enterprise enterprise; 

エンタープライズエンティティ

private String name; 
private String nit; 
private String sheetconfig; 
@OneToMany(cascade = CascadeType.ALL, 
     mappedBy = "enterprise", orphanRemoval = true) 
private List<Branch> branches = new ArrayList<Branch>(); 

私は枝を保存しようとすると、私はコンボボックスで企業を選択し、要求を送信しますが、私は次のエラーを取得:

object references an unsaved transient instance - save the transient instance before flushing : com.gettecob.gestcart.model.Branch.enterprise 

注:企業リストが存在し、唯一の私は枝を保存する必要がある聖霊降臨祭をエンタープライズアソシエーション私はこれを聖霊降臨祭の助けが必要

@Bean(name = "entityManagerFactory") 
public LocalContainerEntityManagerFactoryBean entityManagerFactory(DriverManagerDataSource dataSource) { 

    LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean(); 
    entityManagerFactoryBean.setDataSource(dataSource); 
    entityManagerFactoryBean.setPackagesToScan(new String[]{"com.gettecob.gestcart.*"}); 
    entityManagerFactoryBean.setLoadTimeWeaver(new InstrumentationLoadTimeWeaver()); 
    entityManagerFactoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter()); 

    Map<String, Object> jpaProperties = new HashMap<String, Object>(); 
    jpaProperties.put("hibernate.hbm2ddl.auto", "create"); 
    jpaProperties.put("hibernate.show_sql", "true"); 
    jpaProperties.put("hibernate.format_sql", "true"); 
    jpaProperties.put("hibernate.use_sql_comments", "true"); 
    jpaProperties.put("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect"); 
    entityManagerFactoryBean.setJpaPropertyMap(jpaProperties); 

    return entityManagerFactoryBean; 
} 

BranchRepositoryは方法

public Branch save(Branch branch) { 
    return em.merge(branch); 
} 

を保存:

豆の設定。

+0

企業にはIDが設定されていますか? – ThomasEdwin

+0

@ThomasEdwinはい、エンタープライズオブジェクトにidフィールドが設定されています。 –

+0

エンタープライズを最初に保持してから支店エンティティを維持してみてください。 - 企業がデータベースに存在しないと仮定し、それが存在し、同じエンティティが支店エンティティに設定されている場合、それを維持しながらより深く掘り下げる必要があります。永続的なコードである可能性があり、その設定が問題の特定に役立つ可能性があります。 – Sathyan

答えて

0

Enterpriseが管理されていない理由はわかりません。永続化されていないか、または他の永続性コンテキストから切り離されている可能性があります。

たとえば、永続コンテキストが設定idを処理する必要があるため、新しいエンティティのidを既存のエンティティを模倣するように設定すると発生します。

それはEnterpriseを持つために、その後の前に永続化されていない場合は、あなたがCascadeType.MERGEを有効にするためにもしようとすることができ、その@ManyToOne注釈

@ManyToOne(cascade=CascadeType.PERSIST) 
@JoinColumn(name = "enterprise_id") 
private Enterprise enterprise; 

CascadeType.PERSISTを有効にする必要がBranchを永続化するとき持続しました。それはあなたのEnterpriseがどのような理由でがデタッチされたと考えられるのかに少し依存します

関連する問題