gpt4 book ai didi

java - 数据库没有生成表

转载 作者:行者123 更新时间:2023-12-01 15:41:56 25 4
gpt4 key购买 nike

嗨,我正忙于使用 Hibernate 和 Spring 开发 Java EE 应用程序。我有一个我运行的文章类(class)。但没有为其生成表。控制台中没有错误。

这是 Article 类:

package com.bd.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="Articlet")
public class Article {

int id;
String nom;
String type;
int qte;


public Article() {
super();
// TODO Auto-generated constructor stub
}
@Id
@GeneratedValue
@Column(name="ID")
public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}
@Column(name="Nom")
public String getNom() {
return nom;
}
public void setNom(String nom) {
this.nom = nom;
}
@Column(name="Type")
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
@Column(name="Qunatité")
public int getQte() {
return qte;
}
public void setQte(int qte) {
this.qte = qte;
}




}

和 ArticleDao 类:

package com.bd.dao;

import java.util.Collection;
import java.util.List;



import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.bd.entity.Article;
@Repository
@Transactional
@Configuration
public class ArticleDaoImp implements ArticleDao {

@Autowired
SessionFactory sessionFactory;

@SuppressWarnings("unchecked")
@Transactional(readOnly = true)
public List<Article> getAll() {

return sessionFactory.getCurrentSession().createQuery("from Article")
.list();
}

@Transactional(readOnly = true)
public Article getById(int articleId) {

return (Article) sessionFactory.getCurrentSession().get(Article.class,
articleId);
}

@Override
public void save(Article article) {
sessionFactory.getCurrentSession().merge(article);

}

@Override
public void delete(Article article) {
sessionFactory.getCurrentSession().delete(article);

}

}

这是 hibernateataccesscontext 文件:

<?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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

<!-- Auto-detect the DAOs -->
<context:component-scan base-package="com.bd.dao"/>
<!-- <context:component-scan base-package="com.bd.service"/>
<context:component-scan base-package="com.bd.controleur"/> -->


<context:property-placeholder location="WEB-INF/jdbc.properties"/>


<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${database.driver}" />
<property name="url" value="${database.url}" />
<property name="username" value="${database.user}" />
<property name="password" value="${database.password}" />
</bean>



<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>com.bd.entity.Article</value>

</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<!-- generation base donnée <prop key="hibernate.hbm2ddl.auto">create-drop</prop> -->
<prop key="hibernate.hbm2ddl.auto">create-drop</prop>
</props>
</property>
<property name="articleListeners">
<map>
<entry key="merge">
<bean class="org.springframework.orm.hibernate3.support.IdTransferringMergeArticleListener"/>
</entry>
</map>
</property>
</bean>




<tx:annotation-driven transaction-manager="txnManager"/>

<bean id="txnManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager"
p:sessionFactory-ref="sessionFactory"/>

<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>

</beans>


et jdbc.properties


database.driver=com.mysql.jdbc.Driver
database.url=jdbc:mysql://localhost:3306/ccccc
database.user=root
database.password=root
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
hibernate.show_sql=true

问题是没有生成数据库结构。请帮忙。

最佳答案

在 Hibernate 配置 xml 中,确保将 hibernate.hbm2ddl.auto 设置为 updatecreate创建-删除

关于java - 数据库没有生成表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7917383/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com