【问题标题】:Hibernate autogenerated SQL table is not being created未创建 Hibernate 自动生成的 SQL 表
【发布时间】:2019-04-27 07:33:32
【问题描述】:

大家好,感谢大家阅读我的问题,

我在 Eclipse 的一个 maven 项目中使用 hibernate,我的 dbms 是带有 xampp 的 mysql。我的其中一个类必须是数据库中的表,但没有在数据库中创建,不知道为什么。

javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not extract ResultSet
Caused by: java.sql.SQLSyntaxErrorException: Table 'db_p1_mdai.tuiteos' doesn't exist

这是我的非自动生成类/表的代码。

@Entity
@Table(name = "Tuiteos")
public class TuiteoVO implements Serializable{

private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(generator = "increment")
@GenericGenerator(name="increment", strategy="increment")
private int id_tuiteo;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="id_usuario_fk")
private UsuarioVO usuario_fk;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="id_tuit_fk")
private TuitVO tuit_fk;

private boolean tuit_propio;

private boolean like;

private boolean retuit;

该类的其余部分包括一个哑类、构造函数、默认和参数化、getter、setter、equals 和 toString,所以我将跳过它。

这里是引用的类:

@Entity
@Table(name = "Usuarios")
public class UsuarioVO implements Serializable {

private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(generator = "increment")
@GenericGenerator(name="increment", strategy="increment")
private int id_usuario;

private String nombre;

private String arroba;

private String correo;

private String password;

private String fechaRegistro;

private String descripcion;

@OneToMany(fetch = FetchType.LAZY, mappedBy = "id_usuario_emisor")
private Set<MensajeVO> mensajes;

@OneToMany(fetch = FetchType.LAZY, mappedBy = "usuario_fk")
private Set<TuiteoVO> tuiteos;

与 Mensaje 类的 OneToMany 关系完美运行,所以我不会粘贴代码。

@Entity
@Table(name = "Tuits")
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
public class TuitVO implements Serializable {

private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(generator = "increment")
@GenericGenerator(name="increment", strategy="increment")
protected int id_tuit;

protected String texto;

protected String fecha;

protected String hora;

protected int likes;

protected int retuits;

@ManyToMany(targetEntity = HashtagVO.class, cascade = { CascadeType.PERSIST, CascadeType.MERGE })
@JoinTable(name = "Tuit_Hashtag", joinColumns = @JoinColumn(name = "id_tuitt"), inverseJoinColumns = @JoinColumn(name = "id_hashtagg"))
protected Collection<HashtagVO> lista_hashtags;

@OneToMany(fetch = FetchType.LAZY, mappedBy = "tuit_fk")
protected Set<TuiteoVO> tuiteos;

最后,persistence.xml:

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
    version="2.0">
    <persistence-unit name="es.unex.cum.mdai.*">
        <class>es.unex.cum.mdai.vo.UsuarioVO</class>
        <class>es.unex.cum.mdai.vo.TuitVO</class>
        <class>es.unex.cum.mdai.vo.HashtagVO</class>
        <class>es.unex.cum.mdai.vo.MensajeVO</class>
        <class>es.unex.cum.mdai.vo.TuiteoVO</class>
        <class>es.unex.cum.mdai.vo.TuitRespuestaVO</class>
        <properties>
            <property name="javax.persistence.jdbc.driver"
                value="com.mysql.jdbc.Driver" />
            <property name="javax.persistence.jdbc.url"
                value="jdbc:mysql://localhost:3306/DB_P1_MDAI?serverTimezone=UTC" />
            <property name="javax.persistence.jdbc.user" value="root" />
            <property name="javax.persistence.jdbc.password" value="" />
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.hbm2ddl.auto" value="create" />
        </properties>
    </persistence-unit>
</persistence>

提前致谢。

【问题讨论】:

    标签: java sql eclipse hibernate maven


    【解决方案1】:

    好的,我得到了答案。周末研究了很久,头疼,死心塌地,没有找到解决问题的办法,最后我和老师聊了聊,他看到了此刻的问题,而且,除了我是个白痴之外,问题是。 .(鼓声):

    private boolean like;
    

    我正在对自己进行一种 sql 注入,因为“like”是 SQL 的保留字,“Tuiteos”表没有被创建。我永远不会忘记这次经历。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-11-19
      • 1970-01-01
      • 2020-08-28
      • 2022-08-18
      • 2012-05-30
      • 2017-08-23
      • 2014-12-11
      相关资源
      最近更新 更多