在JPA / Spring Data中,我想将一个对象(DsConfig)添加到CampaignStructure对象的DsConfig列表中。
此列表具有单向多对多关联
\G
CampaignStructure:
@Service
public class CampaignConfigurationService {
@Transactional
public void saveCampaignConfiguration(CampaignConfigurationModel campaignConfigurationModel){
[...]
CampaignStructure campaignStructure=campaignStructureService.findOne(campaignStructureId);
[...]
DsConfig dsConfig=new DsConfig();
dsConfig.setDataSource(dataSourceModel.getDatasource());
dsConfig.setSource(sourceSelected);
dsConfig.setLocation(dataSourceModel.getUrlLocation());
// dsConfig=dsConfigService.save(dsConfig);
campaignStructure.getDsConfigs().add(dsConfig);
[...]
campaignStructureService.save(campaignStructure);
[...]
DsConfig:
@Entity
@Table(name="CAMPAIGN_STRUCTURE")
public class CampaignStructure implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name="CAMPAIGN_STRUCTURE_ID")
private int campaignStructureId;
private String url;
//uni-directional many-to-one association to Campaign
@ManyToOne
@JoinColumn(name="CAMPAIGN_ID")
private Campaign campaign;
//uni-directional many-to-one association to UseCase
@ManyToOne
@JoinColumn(name="USE_CASE_ID")
private UseCase useCase;
//uni-directional many-to-many association to DsConfig
@ManyToMany
@JoinTable(
name="CAMPAIGN_STRUCTURE_DS_CONFIG"
, joinColumns={
@JoinColumn(name="CAMPAIGN_STRUCTURE_ID")
}
, inverseJoinColumns={
@JoinColumn(name="DS_CONFIG_ID")
}
)
private List<DsConfig> dsConfigs;
[...]
当我运行saveCampaignConfiguration方法时,我收到此错误:
@Entity
@Table(name="DS_CONFIG")
@NamedQuery(name="DsConfig.findAll", query="SELECT d FROM DsConfig d")
public class DsConfig implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name="DS_CONFIG_ID")
private int dsConfigId;
@Column(name="DATA_IMPORT")
private String dataImport;
private String location;
//uni-directional many-to-one association to DataSource
@ManyToOne
@JoinColumn(name="DATA_SOURCE_ID")
private DataSource dataSource;
//uni-directional many-to-many association to DsData
@ManyToMany
@JoinTable(
name="DS_CONFIG_DS_DATA"
, joinColumns={
@JoinColumn(name="DS_CONFIG_ID")
}
, inverseJoinColumns={
@JoinColumn(name="DS_DATA_ID")
}
)
private List<DsData> dsData;
//uni-directional many-to-one association to Source
@ManyToOne
@JoinColumn(name="SOURCE_ID")
private Source source;
如果我在添加到列表之前保存dsConfig对象,则会出现此其他错误。我认为问题再次出现在id = 0。
16:46:52,539 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/MyProject].[appServlet]] (http-localhost-127.0.0.1-8080-1) Servlet.service() for servlet appServlet threw exception: javax.persistence.EntityNotFoundException: Unable to find com.myorg.entity.DsConfig with id 0
at org.hibernate.ejb.Ejb3Configuration$Ejb3EntityNotFoundDelegate.handleEntityNotFound(Ejb3Configuration.java:154) [hibernate-entitymanager-4.0.1.Final.jar:4.0.1.Final]
at org.hibernate.event.internal.DefaultLoadEventListener.load(DefaultLoadEventListener.java:210) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]
感谢。
修改
添加了
16:49:25,837 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/MyProject].[appServlet]] (http-localhost-127.0.0.1-8080-1) Servlet.service() for servlet appServlet threw exception: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (`mydb`.`campaign_structure_ds_config`, CONSTRAINT `fk_CAMPAIGN_STRUCTURE_DS_CONFIG_DS_CONFIG1` FOREIGN KEY (`DS_CONFIG_ID`) REFERENCES `ds_config` (`DS_CONFIG_ID`) ON DELETE NO ACTION ON U)
到DsConfig实体定义;
现在,如果我之前保存DsConfig以保存CampaignStructure,它就可以了。