Моего этот вопрос следить за вопросом JPA+one-to-many and many-to-one using join table or third tableJPA + много-к-одному целевые отношений объекта сохраняются в таблице
Я ссылающийся на Query
в Response
сущности. Когда я создаю запись для Response
. Он также создает запись в таблице mydb.tbl_query
. Мое требование состоит в том, что когда я вставляю объект Response
, он должен вставить запись в mydb.tbl_response
и создать запись в mydb.tbl_map_query_response
путем ссылки query_id
.
public class ResponseDAOImplTests extends BaseDAOImplTests {
@Autowired
@Qualifier("queryDAO")
private QueryDAO queryDAO;
@Autowired
@Qualifier("responseDAO")
private ResponseDAO responseDAO;
/**
* Query reference.
*/
private Query query = null;
/**
* <p>
* Adapter for earlier versions of JUnit.
* </p>
*
* @return a test suite.
*/
public static junit.framework.Test suite() {
return new JUnit4TestAdapter(ResponseDAOImplTests.class);
}
@Before
public void setUp() throws QADAOException {
user = accountDAO.getUser("[email protected]");
query = queryDAO.getQuery(2);
}
@Test
public void createResponse() throws QADAOException {
Response response = new Response();
response.setQuery(query);
response.setResponse("ResponseTest1");
responseDAO.postResponse(response);
}
}
public class ResponseDAOImpl extends BasePersitenceDAO implements ResponseDAO {
@Override
@Transactional
public void postResponse(Response response) throws QADAOException {
getEntityManager().persist(response);
getEntityManager().flush();
}
}
public abstract class BasePersitenceDAO {
@PersistenceContext
private EntityManager entityManager;
protected BasePersitenceDAO() {
// Empty
}
protected EntityManager getEntityManager() {
return entityManager;
}
public void setEntityManager(EntityManager entityManager) {
this.entityManager = entityManager;
}
}
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<context:annotation-config />
<tx:annotation-driven transaction-manager="transactionManager" />
<context:component-scan base-package="com.qa" />
<context:property-placeholder location="classpath:config.properties" />
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
<property name="persistenceUnitName" value="persistenceUnit" />
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<bean id="basePersitenceDAO" abstract="true" class="com.qa.dao.impl.BasePersitenceDAOImpl" />
<bean id="queryDAO" class="com.qa.dao.impl.QueryDAOImpl" parent="basePersitenceDAO" />
<bean id="responseDAO" class="com.qa.dao.impl.ResponseDAOImpl" parent="basePersitenceDAO" />
</beans>
Мой вопрос «не следует вводить еще одну запись в mydb.tbl_query
».
И ваш вопрос ...? –
См. Последнюю строку. –
В другом вопросе у Response есть много-к-одному для запроса с каскадом. Если вы сохраняете ответ, persist будет каскадироваться в объект Query, на который ссылается. То же самое происходит при слиянии и удалении вызовов. Он не должен вставлять новый объект Query в таблицу запросов, хотя, если он уже существует, возможно, вы создаете новый объект запроса. Вы должны найти существующую, если не хотите, чтобы новая запись была добавлена в таблицу запросов. – Chris