我有一个Spring项目,用于在Intellij IDEA中设置的小型Web应用程序.
它在Hibernate之上使用JPA作为持久层.数据源(MySQL)在Spring应用程序上下文中定义:
<!-- Values are configured via the property override -->
<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" >
<property name="driverClassName" value=""/>
<property name="url" value=""/>
<property name="username" value=""/>
<property name="password" value=""/>
</bean>
Run Code Online (Sandbox Code Playgroud)
实际值从属性文件中读取,并在Spring运行时使用属性覆盖机制注入.
然后将数据源注入到同一应用程序上下文中的实体管理器工厂中:
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="myDataSource"/>
</bean>
Run Code Online (Sandbox Code Playgroud)
最后,实体管理器使用注释注入DAO:
/**
* Shared, thread-safe proxy for the actual transactional EntityManager
*/
@PersistenceContext
private EntityManager em;
Run Code Online (Sandbox Code Playgroud)
当我构建并部署到Tomcat时,一切正常,但Intellij的JPA验证似乎并不了解从何处获取数据源.
在我的实体中,表的名称和列的名称用红色下划线,验证消息是"无法解析表"或"无法解析列":
@Entity
@Table(name = "domain")
public class Domain extends AbstractAgendaEntity {
Run Code Online (Sandbox Code Playgroud)
在这个例子中,它"domain"是不被认为有效的部分.
我已经在"数据库"工具窗口中手动配置了我的数据库,我可以在控制台中看到我的表并执行SQL查询.
如何告诉Intellij使用此数据源来解析JPA实体的表名?
这是我的repository界面:
public interface ContentRepository extends JpaRepository<Content, Long> {
@Query(value = "select c from Content c where c.ContentCategory.genre = :genre and c.ContentType.genre = :contentType")
Iterable<Content> findByTypeAndCategory(@Param("contentType") String contentType, @Param("genre") String genre);
}
Run Code Online (Sandbox Code Playgroud)
这是ContentPOJO:
@Entity
@Table(name = "content")
public class Content implements Serializable {
public Content() {
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@ManyToOne
private ContentCategory contentCategory;
@ManyToOne
private ContentType contentType;
// other methods }
Run Code Online (Sandbox Code Playgroud)
在这里我的applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jpa="http://www.springframework.org/schema/data/jpa" …Run Code Online (Sandbox Code Playgroud)