未创建Hibernate自动生成的SQL表



嗨,感谢大家阅读我的文章,

我在eclipse中的一个maven项目中使用hibernate,我的dbms是带examplep的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;

类的其余部分由一个哑类、构造函数、默认和参数化、getters、setters、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>

提前谢谢。

好的,我得到了答案。经过周末长时间的研究,头痛、想死、找不到解决问题的办法,最后我和老师聊了聊,他看到了这个问题,而且,除了我是个白痴之外,这个问题。。(卷筒(:

private boolean like;

我对自己做了一种sql注入,因为"like"是sql的保留词,"Tuiteos"表没有创建。我永远不会忘记这段经历。

最新更新