gpt4 book ai didi

java - Hibernate不创建表

转载 作者:行者123 更新时间:2023-12-01 17:07:51 25 4
gpt4 key购买 nike

我是 Hibernate Annotations 的新手,我想尝试一个示例。

我有两个类(Node 和 HyperEdge),当我运行应用程序时,它只为 Node 创建一个表,而不为 HyperEdge 创建一个表。

这是我开发的代码:

节点:

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

@Entity
@Table(name="Node")
public class Node {

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Integer id;

@Column
private String name;

@Column(name="\"group\"")
private Integer group;

public Node() {
super();
// TODO Auto-generated constructor stub
}

public Node(Integer id, String name, Integer group) {
super();
this.id = id;
this.name = name;
this.group = group;
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Integer getGroup() {
return group;
}

public void setGroup(Integer group) {
this.group = group;
}

}

超边缘:

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

@Table(name="HyperEdge")
public class HyperEdge {

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Integer id;

@Column
private String title;

public HyperEdge() {
super();
// TODO Auto-generated constructor stub
}
public HyperEdge(Integer id, String title) {
super();
this.id = id;
this.title = title;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}

}

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>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/exhiber</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>

<property name="connection.pool_size">1</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="current_session_context_class">thread</property>
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">create</property>

<mapping class="com.hib.ex.entity.Node" />
<mapping class="com.hib.ex.entity.HyperEdge" />

</session-factory>

</hibernate-configuration>

HibernateDao:

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.hib.ex.entity.HyperEdge;
import com.hib.ex.entity.Node;

public class HibExDao {

public void saveNode(Node noeud) {

SessionFactory sf = HibExUtil.getSessionFactory();
Session session = sf.openSession();
session.beginTransaction();

session.save(noeud);

session.getTransaction().commit();
session.close();

}

public List listNode() {

SessionFactory sf = HibExUtil.getSessionFactory();
Session session = sf.openSession();

List nodes = session.createQuery("FROM Node").list();
session.close();
return nodes;

}

public Node readNode(Integer id) {

SessionFactory sf = HibExUtil.getSessionFactory();
Session session = sf.openSession();

Node noeud = (Node) session.get(Node.class, id);
session.close();
return noeud;

}

public void saveHyperEdge(HyperEdge he, String chaine) {

SessionFactory sf = HibExUtil.getSessionFactory();
Session session = sf.openSession();
session.beginTransaction();

he.setTitle(chaine);

session.save(he);
session.getTransaction().commit();
session.close();

}

public List listHyperEdge() {

SessionFactory sf = HibExUtil.getSessionFactory();
Session session = sf.openSession();

List hyperedges = session.createQuery("FROM HyperEdge").list();
session.close();
return hyperedges;

}

public HyperEdge readHyperEdge(Integer id) {

SessionFactory sf = HibExUtil.getSessionFactory();
Session session = sf.openSession();

HyperEdge hyperEdge = (HyperEdge) session.get(HyperEdge.class, id);
session.close();
return hyperEdge;

}

}

主类:

import java.util.List;

import com.hib.ex.dao.HibExDao;
import com.hib.ex.entity.HyperEdge;
import com.hib.ex.entity.Node;

public class Run {


public static void main(String[] args) {

HibExDao dao = new HibExDao();

System.out.println("****************WRITING****************");
Node n1 = new Node();
n1.setName("toto");
dao.saveNode(n1);
System.out.println("Node saved!");
Node n2 = new Node();
n2.setName("lala");
dao.saveNode(n2);
System.out.println("Node saved!");

System.out.println("\n****************READING****************");
List nodes = dao.listNode();
System.out.println("Name in Node number 2 is: " + dao.readNode(2).getName());

}

}

有什么问题吗?我该如何解决它?

谢谢!

最佳答案

也许您必须向 HyperEdge 类添加 @Entity 注释

关于java - Hibernate不创建表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24637365/

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