我正在学习使用Spring&冬眠。现在我在hibernate中遇到了许多关系的问题。我的数据没有保存在jon表中。我不知道为什么。请有人帮帮我
我有两个域类叫做Projects&技术。
这是我在这两个班级中的映射
项目类: -
@ManyToMany
@JoinTable(name="project_technology",
joinColumns={@JoinColumn(name="projectId")},
inverseJoinColumns={@JoinColumn(name="technologyId")})
public List<Technology> getTechnology() {
return technology;
}
技术课: -
@ManyToMany
@JoinTable(name="project_technology",
joinColumns={@JoinColumn(name="technologyId")},
inverseJoinColumns={@JoinColumn(name="projectId")})
public List<Projects> getProjects() {
return projects;
}
在我的DAo图层
getSession().beginTransaction();
getHibernateTemplate().save(projects);
getHibernateTemplate().clear()
用于保存数据。它没有显示错误,但数据未保存在连接表中。
当我使用
时Session session = getSession();
Transaction transaction = session.beginTransaction();
session.save(projects);
transaction.commit();
session.close();
保存,数据正确保存在连接表中。但保存后我得到了这个错误
org.springframework.transaction.TransactionSystemException: Could not commit Hibernate transaction; nested exception is org.hibernate.TransactionException: Transaction not successfully started
我的applicationcontext.xml
<bean id="mySessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="hibernateDataSourse" />
<property name="annotatedClasses">
<list>
<value>com.sla.tecprof.domain.Technology</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect"> org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>
<bean id="transactionProxy"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
abstract="true" lazy-init="true">
<property name="transactionManager" ref="hibernateTransactionManager" />
<property name="transactionAttributeSource" ref="transactionAttributeSource" />
<property name="preInterceptors">
<list>
<ref bean="exceptionInterceptor" />
<ref bean="TransactionPointCutAdvisor" />
</list>
</property>
<property name="postInterceptors">
<list>
<ref bean="hibernateInterceptor" />
</list>
</property>
<property name="proxyTargetClass" value="true" />
</bean>
答案 0 :(得分:0)
您应该只在一侧使用@JoinColum Annotation。
@ManyToMany
@JoinTable(name="project_technology",
joinColumns={@JoinColumn(name="projectId")},
inverseJoinColumns={@JoinColumn(name="technologyId")})
public List<Technology> getTechnology() {
return technology;
}
@ManyToMany(fetch = FetchType.LAZY, mappedBy = "technology")
public List<Projects> getProjects() {
return projects;
}
Hibernate注释的重要资源:http://www.mkyong.com/hibernate/hibernate-many-to-many-relationship-example-annotation/
答案 1 :(得分:0)
我找到了解决方案。这就是我在DAO leyer中所做的事情
Session session = getHibernateTemplate().getSessionFactory().openSession();
Transaction transaction = session.beginTransaction();
session.save(projects);
transaction.commit();
session.close();