JPA,Hibernate和Spring集成:表不存在错误

时间:2021-01-21 08:19:42

I just started learning about JPA, and I have a spring application using hibernate. I'm trying to do a test by inserting a simple date into mysql database, but I'm getting this error message :

我刚开始学习JPA,我有一个使用hibernate的spring应用程序。我试图通过在mysql数据库中插入一个简单的日期来进行测试,但我收到此错误消息:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'banquedb.clients' doesn't exist

This is my test code :

这是我的测试代码:

public class Test {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = 
                new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});
        IBanqueService service = (IBanqueService) context.getBean("service");
        service.addClient(new Client("Ichigo", "AD1"));
        service.addClient(new Client("Kirito", "AD2"));
    }
}

And these my application files:

这些我的应用程序文件:

persistence.xml:

<persistence-unit name="sample" transaction-type="RESOURCE_LOCAL">
  <provider>org.hibernate.ejb.HibernatePersistence</provider>
  <properties>
    <property name="hibernate.show_sql" value="true"/>
    <property name="hibernate.hbm2ddl.auto" value="create"/>
   </properties>
  </persistence-unit>
</persistence>

applicationContext.xml

<bean id="dao" class="org.gestion.banque.dao.BanqueDAOImpl"></bean>
    <bean id="service" class="org.gestion.banque.service.BanqueServiceImpl">
        <property name="dao" ref="dao"></property>
    </bean>
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://localhost:3306/banquedb"></property>
        <property name="username" value="root"></property>
        <property name="password" value=""></property>
    </bean>
    <bean id="persistenceUnitManager" class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
        <property name="persistenceXmlLocations">
            <list>
                <value>classpath*:META-INF/persistence.xml</value>
            </list>
        </property>
        <property name="defaultDataSource" ref="dataSource"></property>
    </bean>
    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="persistenceUnitManager" ref="persistenceUnitManager"></property>
        <property name="persistenceUnitName" value="sample"></property>
    </bean>
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory"></property>
    </bean>
    <tx:annotation-driven transaction-manager="transactionManager"/>
    <context:annotation-config></context:annotation-config>

Client.java

@Entity
@Table(name="CLIENTS")
public class Client implements Serializable {
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Long codeClient;
    private String nomClient;
    private String adresseClient;
    @OneToMany(mappedBy="client",fetch=FetchType.LAZY)
    private Collection<Compte> comptes;
    public Long getCodeClient() {
        return codeClient;
    }
    public void setCodeClient(Long codeClient) {
        this.codeClient = codeClient;
    }
    public String getNomClient() {
        return nomClient;
    }
    public void setNomClient(String nomClient) {
        this.nomClient = nomClient;
    }
    public String getAdresseClient() {
        return adresseClient;
    }
    public void setAdresseClient(String adresseClient) {
        this.adresseClient = adresseClient;
    }
    public Collection<Compte> getComptes() {
        return comptes;
    }
    public void setComptes(Collection<Compte> comptes) {
        this.comptes = comptes;
    }
    public Client() {
        super();
    }
    public Client(String nomClient, String adresseClient) {
        super();
        this.nomClient = nomClient;
        this.adresseClient = adresseClient;
    }

}

1 个解决方案

#1


I would suggest to change the table name to the correct case in the Client entity class.

我建议将表名更改为Client实体类中的正确大小写。

Use @Table(name="clients") instead of @Table(name="CLIENTS").

使用@Table(name =“clients”)而不是@Table(name =“CLIENTS”)。

#1


I would suggest to change the table name to the correct case in the Client entity class.

我建议将表名更改为Client实体类中的正确大小写。

Use @Table(name="clients") instead of @Table(name="CLIENTS").

使用@Table(name =“clients”)而不是@Table(name =“CLIENTS”)。