表有注释但没有在Hibernate中映射

时间:2018-05-19 04:10:26

标签: java spring hibernate spring-boot

表未映射[[From table]],其根本原因表没有映射错误

来自 src / main / java

下的 com.springboot.pojos.Hero
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;

@Entity
@Table(name="HEROES")
public class Hero

这是来自 src / main / resources

下的 hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
    <property name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property>
    <property name="hibernate.connection.password">password</property>
    <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
    <property name="hibernate.connection.username">username</property>
    <property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
   <property name="hibernate.show_sql">true</property> 
    <mapping class="com.springboot.pojos.Hero"/>
    <mapping class="com.springboot.pojos.Abilities"/>

</session-factory>
</hibernate-configuration>

我正在使用春季靴子

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.2.RELEASE</version>
</parent>

<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        </dependency>
</dependencies>

更新================================

2018-05-18 21:44:59.817  INFO 2992 --- [o-auto-1-exec-1]     o.h.h.i.QueryTranslatorFactoryInitiator  : HHH000397: Using ASTQueryTranslatorFactory
2018-05-18 21:45:00.399 ERROR 2992 --- [o-auto-1-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: Hero is not mapped [FROM Hero]] with root cause

org.hibernate.hql.internal.ast.QuerySyntaxException: Hero is not mapped

查询错误

 String hql = "FROM Hero";
 Query query = currSession.createQuery(hql);

2 个答案:

答案 0 :(得分:0)

是的,使用Hibernate注释,您可以使用上面显示的方式将实体映射到表。无需配置任何其他文件。请提供有关您遇到的问题的更多信息。

答案 1 :(得分:0)

如果您正在使用sprint引导,则需要配置数据源并通过@EnableJpaRepositories告知spirng引导.witch是包含实体的包。

在application.property中,您必须设置数据源变量 https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(entityManagerFactoryRef = "entityManagerFactory", basePackages = {
        "com.getsoft.invapp.repositoty" })
//@PropertySource("classpath:application.properties")
public class PersistenceJPAConfig {

    @Autowired
    private Environment env;

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
        LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
        em.setDataSource(dataSource());
        em.setPackagesToScan(new String[] { "com.getsoft.invapp.entity" });

        JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        em.setJpaVendorAdapter(vendorAdapter);
        em.setJpaProperties(additionalProperties());

        return em;
    }

    @Bean(name = "dataSource")
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource dataSource() {

        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName(env.getProperty("spring.datasource.driver-class-name"));
        dataSource.setUrl(env.getProperty("spring.datasource.url"));
        dataSource.setUsername(env.getProperty("spring.datasource.username"));
        dataSource.setPassword(env.getProperty("spring.datasource.password"));
        return dataSource;

    }