我正在开发一个Spring / hibernate项目,并且遇到@ManyToMany条件的问题。 联接表spot_topo是在加载应用程序时创建的,同时包含了Spots_id和topos_id字段,但插入无效(没有错误,仅插入了任何内容)。 我尝试添加@JoinTable,但随后编译失败。 知道会出现什么错误吗? 谢谢!
现货实体
@ManyToMany( fetch=FetchType.EAGER, cascade=CascadeType.ALL )
私有列表主题;
Topo实体
@ManyToMany(
mappedBy = "topos", fetch = FetchType.EAGER, cascade = {CascadeType.ALL})
private List<Spot> spots = new ArrayList<>();
休眠配置
@Bean
public LocalSessionFactoryBean sessionFactory() {
LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
sessionFactory.setDataSource(dataSource());
sessionFactory.setPackagesToScan("org.example.demo.climb.consumer");
sessionFactory.setPackagesToScan("org.example.demo.climb.model");
/* sessionFactory.setPackagesToScan("org.example.demo.climb.webapp");*/
Properties hProperties = hibernateProperties();
hProperties.setProperty("show_sql", "true");
sessionFactory.setHibernateProperties(hProperties);
return sessionFactory;
}
@Bean
public DataSource dataSource() {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName("org.postgresql.Driver");
dataSource.setUrl("jdbc:postgresql://127.0.0.1:5432/ClimbingWorld");
dataSource.setUsername("ocp");
dataSource.setPassword("123");
return dataSource;
}
@Bean
public PlatformTransactionManager hibernateTransactionManager() {
HibernateTransactionManager transactionManager
= new HibernateTransactionManager();
transactionManager.setSessionFactory(sessionFactory().getObject());
return transactionManager;
}
private final Properties hibernateProperties() {
Properties hibernateProperties = new Properties();
hibernateProperties.setProperty(
"hibernate.hbm2ddl.auto", "update");
hibernateProperties.setProperty(
"hibernate.dialect", "org.hibernate.dialect.PostgreSQL82Dialect");
hibernateProperties.setProperty("show_sql", "true");
return hibernateProperties;
}
当我尝试添加时:
@ManyToMany( fetch=FetchType.EAGER, cascade=CascadeType.ALL )
@JoinTable(name = "spot_topo",
joinColumns = { @JoinColumn(name = "topos_id",
nullable = false, updatable = true) },
inverseJoinColumns = { @JoinColumn(name = "spots_id",
nullable = false, updatable = true) })
private List<Topo> topos;
我遇到编译错误:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'loginAction': Unsatisfied dependency expressed through field 'memberManager'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'memberManager': Unsatisfied dependency expressed through field 'memberDao'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'memberDaoImpl': Unsatisfied dependency expressed through field 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in org.example.demo.climb.consumer.config.HibernateConf: Invocation of init method failed; nested exception is java.lang.NoSuchMethodError: javax.persistence.JoinTable.indexes()[Ljavax/persistence/Index;
dependencies(Beans)
<?xml version="1.0"?>
http://maven.apache.org/xsd/maven-4.0.0.xsd” xmlns =“ http://maven.apache.org/POM/4.0.0”> 4.0.0
<!-- =============================================================== -->
<!-- Informations du projet -->
<!-- =============================================================== -->
<!-- ===== Projet Parent Maven ===== -->
<parent>
<groupId>org.example.demo</groupId>
<artifactId>climb</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<properties>
<hibernate.version>5.3.5.Final</hibernate.version>
<spring.version>4.3.11.RELEASE</spring.version>
</properties>
<!-- ===== Informations Maven ===== -->
<artifactId>climb-model</artifactId>
<!-- ===== Informations générales ===== -->
<name>${projectName} - Model</name>
<!-- =============================================================== -->
<!-- Dépendances -->
<!-- =============================================================== -->
<dependencies>
<!-- ===== Bibliothèques tierces ===== -->
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate.javax.persistence/hibernate-jpa-2.0-api -->
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
<version>1.0.0.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.postgresql/postgresql -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/net.sf.ehcache/ehcache-core -->
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-core</artifactId>
<version>2.6.11</version>
</dependency>
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-ehcache -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-ehcache</artifactId>
<version>${hibernate.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.0.8.RELEASE</version>
</dependency>
<!-- ===== Tests ===== -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
</dependencies>
依赖项(主要)
<?xml version="1.0" encoding="UTF-8"?>
http://maven.apache.org/xsd/maven-4.0.0.xsd“> 4.0.0
<!-- =============================================================== -->
<!-- Informations du projet -->
<!-- =============================================================== -->
<!-- ===== Informations Maven ===== -->
<groupId>org.example.demo</groupId>
<artifactId>climb</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!--<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>org.eclipse.persistence.core</artifactId>
<version>2.6.4</version>
</dependency>-->
</dependencies>
<packaging>pom</packaging>
<!-- ===== Informations générales ===== -->
<name>${projectName}</name>
<url>http://example.org</url>
<description>
Le super projet de gestion de climbs d'incident et de demandes d'évolution !
</description>
<organization>
<name>Mon Entreprise</name>
<url>https://exemple.org</url>
</organization>
<!-- =============================================================== -->
<!-- DistributionManagement -->
<!-- =============================================================== -->
<distributionManagement>
<site>
<id>site-projet</id>
<url>scp://localhost/tmp/</url>
</site>
</distributionManagement>
<!-- =============================================================== -->
<!-- Propriétés -->
<!-- =============================================================== -->
<properties>
<projectName>climb</projectName>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<spring.version>5.0.8.RELEASE</spring.version>
<struts.version>2.5.16</struts.version>
</properties>
<!-- =============================================================== -->
<!-- Modules -->
<!-- =============================================================== -->
<modules>
<module>climb-webapp</module>
<module>climb-business</module>
<module>climb-consumer</module>
<module>climb-model</module>
<module>climb-technical</module>
</modules>
<!-- =============================================================== -->
<!-- Gestion des dépendances -->
<!-- =============================================================== -->
<dependencyManagement>
<dependencies>
<!-- ===== Modules ===== -->
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>climb-webapp</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>climb-business</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>climb-consumer</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>climb-model</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>climb-technical</artifactId>
<version>${project.version}</version>
</dependency>
<!-- ===== Bibliothèques tierces ===== -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.3.7.Final</version>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.1.0.Final</version>
</dependency>
<dependency>
<groupId>org.apache.bval</groupId>
<artifactId>bval-jsr</artifactId>
<version>1.1.2</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.1</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.6</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-framework-bom</artifactId>
<version>${spring.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- JSR-250 -->
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.2</version>
</dependency>
<!-- JSR-330 -->
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
<!-- Gestion des logs avec Log4j -->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.9.1</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-jcl</artifactId>
<version>2.9.1</version>
<scope>runtime</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.postgresql/postgresql -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.struts/struts2-bom -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-bom</artifactId>
<version>${struts.version}</version>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
<!-- =============================================================== -->
<!-- Build -->
<!-- =============================================================== -->
<build>
<!-- ===== Gestion des plugins ===== -->
<pluginManagement>
<plugins>
<!-- Packaging JAR -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<!-- Compilation des sources Java -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<showDeprecation>true</showDeprecation>
<showWarnings>true</showWarnings>
</configuration>
</plugin>
<!-- Packaging WAR -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.0.0</version>
</plugin>
<!-- Assemblage -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.0.0</version>
</plugin>
<!-- Site -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<version>3.6</version>
<configuration>
<locales>fr</locales>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
<!-- =============================================================== -->
<!-- Gestion des rapports -->
<!-- =============================================================== -->
<reporting>
<plugins>
<!-- ===== Rapport d'information général sur le projet ===== -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>2.7</version>
</plugin>
<!-- ===== Rapport sur les tests ===== -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-report-plugin</artifactId>
<version>2.20</version>
<configuration>
<linkXRef>false</linkXRef>
</configuration>
<reportSets>
<!-- reportSet d'agrégation des rapports des sous-projets (modules) -->
<reportSet>
<reports>
<report>report</report>
</reports>
<!-- ne pas exécuter ce sous-rapport dans les sous-projets -->
<inherited>false</inherited>
<configuration>
<aggregate>true</aggregate>
</configuration>
</reportSet>
<!-- reportSet non agrégé, exécuté dans tous les sous-projets (modules) -->
<reportSet>
<id>modules</id>
<!-- exécuter ce sous-rapport dans les sous-projets -->
<inherited>true</inherited>
<reports>
<report>report</report>
</reports>
<configuration>
<aggregate>false</aggregate>
</configuration>
</reportSet>
</reportSets>
</plugin>
<!-- ===== Génération de la Javadoc ===== -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.9</version>
<configuration>
<quiet>true</quiet>
<locale>fr</locale>
<skip>true</skip>
</configuration>
<reportSets>
<!-- reportSet exécuté dans tous les modules -->
<reportSet>
<reports>
<report>javadoc</report>
</reports>
</reportSet>
<!-- reportSet d'agrégation des rapports des sous-modules -->
<reportSet>
<id>aggregate</id>
<inherited>false</inherited>
<reports>
<report>aggregate</report>
</reports>
</reportSet>
</reportSets>
</plugin>
<!-- ===== Rapport d'analyse du code par Checkstyle ===== -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>2.17</version>
<configuration>
<configLocation>src/build/checkstyle.xml</configLocation>
<linkXRef>false</linkXRef>
</configuration>
<reportSets>
<!-- reportSet exécuté dans tous les modules -->
<reportSet>
<reports>
<report>checkstyle</report>
</reports>
</reportSet>
<!-- reportSet d'agrégation des rapports des sous-modules -->
<reportSet>
<id>checkstyle-aggregate</id>
<inherited>false</inherited>
<configuration>
<skipExec>true</skipExec>
</configuration>
<reports>
<report>checkstyle-aggregate</report>
</reports>
</reportSet>
</reportSets>
</plugin>
</plugins>
</reporting>
对于插入,我使用具有@Transactional的Classe SpotManager并将DAO类引用为:`@Override 公共无效updateTopo(Topo topo){
topoDao.update(topo);
}
那么DAO是:
public void update(Topo) {
sessionFactory.getCurrentSession().update(o);
}
例如,如果我更新topo.name,那么没问题,它写在Db中。但是,如果我通过了topo.spots,则不会出错,但是也不会写入任何内容。
答案 0 :(得分:0)
更新了Hibernate依赖项后,您的类路径上还有一个旧版本的javax.persistence-api
,该版本未定义方法
javax.persistence.JoinTable.indexes()
。
答案 1 :(得分:0)
您可以在Entity类中使用@JoinColumn批注连接两个表。
@OneToOne(cascade=CascadeType.ALL,fetch=FetchType.EAGER)
@JoinColumn(name="id")
private SpotTopoEntity spotTopo;
答案 2 :(得分:0)
首先,必须将版本从1.0.1.Final降级到1.0.0.Final (消除了编译错误)@selaron,然后另一个问题(未插入)是由于我是将斑点插入到topo对象而没有将topos插入到斑点对象这一事实。 我相应地更新了我的开发人员,并将其插入联接表可以正常工作。 谢谢大家!