gpt4 book ai didi

java - JPA。 JoinTable 和两个 JoinColumns

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:18:49 27 4
gpt4 key购买 nike

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

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)
},
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。 JoinTable 和两个 JoinColumns,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8572072/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com