我尝试使用hibernate连接到Mysql。我生成一个支持Intellij JPA的实体,即使我在Unknown entity: com.UsersEntity
中声明了映射,我也会收到hibernate.cfg.xml
。
实体:
package com;
import javax.persistence.*;
@Entity
@Table(name = "users", schema = "", catalog = "protein_tracker")
public class UsersEntity {
private int id;
private String name;
private int total;
private int goal;
@Id
@Column(name = "id", nullable = false, insertable = true, updatable = true)
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Basic
@Column(name = "name", nullable = false, insertable = true, updatable = true, length = 45)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Basic
@Column(name = "total", nullable = false, insertable = true, updatable = true)
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
@Basic
@Column(name = "goal", nullable = false, insertable = true, updatable = true)
public int getGoal() {
return goal;
}
public void setGoal(int goal) {
this.goal = goal;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
UsersEntity that = (UsersEntity) o;
if (id != that.id) return false;
if (total != that.total) return false;
if (goal != that.goal) return false;
if (name != null ? !name.equals(that.name) : that.name != null) return false;
return true;
}
@Override
public int hashCode() {
int result = id;
result = 31 * result + (name != null ? name.hashCode() : 0);
result = 31 * result + total;
result = 31 * result + goal;
return result;
}
}
配置文件:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/protein_tracker</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.default_schema">protein_tracker</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">AmPiramide69</property>
<mapping class="com.UsersEntity" />
<mapping resource="com/UsersEntity.hbm.xml"/>
<!-- DB schema will be updated if needed -->
<!-- <property name="hbm2ddl.auto">update</property> -->
</session-factory>
</hibernate-configuration>
创建会话工厂:
package com.simpleprogrammer;
import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
public class HibernateUtilities {
private static SessionFactory sessionFactory;
private static ServiceRegistry serviceRegistry;
static {
try {
Configuration configuration = new Configuration().configure();
serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
}
catch (HibernateException exeption) {
exeption.printStackTrace();
System.out.println("Problem creating session factory!");
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
我该如何使用它:
package com.simpleprogrammer;
import com.UsersEntity;
import org.hibernate.Session;
public class Main {
public static void main(String[] args) {
Session session = HibernateUtilities.getSessionFactory().openSession();
session.beginTransaction();
UsersEntity usersEntity = new UsersEntity();
usersEntity.setName("Name");
usersEntity.setTotal(20);
session.save(usersEntity);
session.getTransaction().commit();
session.close();
HibernateUtilities.getSessionFactory().close();
}
}
你知道这是什么问题吗?
答案 0 :(得分:0)
在构建SessionFactory时尝试使用AnnotationConfiguration()而不是Configuration()。
然后你只需要'mapping class =“”'标签。 (而不是hibernate配置文件中的'mapping resource =“”'标签)。