- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 hibernate 新手(上周刚刚开始),我正在开发一个医疗记录系统。我正在使用 Hibernate 4.3 和 MySQL,目前我的 hbm2ddl.auto 属性设置为 create-drop,尽管我之前使用过 update。
我的问题是,即使我只有一个声明
private static SessionFactory sessionFactory = Connect.getSessionFactory();
当我进行多次插入时,再次重用此 sessionFactory 变量,它总是按模式删除。但是,hibernate 文档指出,create-drop 只应在以下情况下删除架构:显式调用 sessionFactory.close();
import java.util.List;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
public class ConcreteMedicalDAO implements MedicalDAO
{
private static SessionFactory sessionFactory = Connect.getSessionFactory();
public void createDoctor(Doctor d)
{
Session session = sessionFactory.openSession();
session.beginTransaction();
session.save(d);
session.getTransaction().commit();
session.close();
}
public void retrieveDoctor(String firstName, String lastName)
{
String hql = "Select * from Doctor where firstName = :firstName and lastName = :lastName";
Session session = sessionFactory.openSession();
session.beginTransaction();
SQLQuery query = session.createSQLQuery(hql).addEntity(Doctor.class);
query.setParameter("firstName", firstName);
query.setParameter("lastName", lastName);
List<Doctor> doctors = query.list();
for(Doctor d: doctors)
{
System.out.println("Address: " + d.getAddress());
System.out.println("Birthdate: " + d.getBirthDate());
System.out.println("Degree: " + d.getDegree());
System.out.println("First name: " + d.getFirstName());
System.out.println("Gender: " + d.getGender());
System.out.println("Last name: " + d.getLastName());
System.out.println("Specialty: " + d.getSpecialty());
System.out.println();
}
session.getTransaction().commit();
session.close();
}
}
正如你所看到的,我没有调用sessionFactory.close(),但是我的数据库每次仍然被清除。任何帮助将不胜感激。
编辑:这是我在应用程序中使用的所有类。
import javax.persistence.*;
@Entity
@Table(name="doctor")
@AttributeOverrides({
@AttributeOverride(name="firstname", column=@Column(name="firstName")),
@AttributeOverride(name="lastname", column=@Column(name="lastName")),
})
public class Doctor extends Person
{
@Id
@GeneratedValue
private int dId;
@Column(name="firstName")
private String firstName;
@Column(name="lastName")
private String lastName;
@Column(name="specialty")
private String specialty;
@Column(name="degree")
private String degree;
@Column(name="gender")
private String gender;
@Column(name="Address")
private String address;
@Column(name="birthDate")
private String birthDate;
@Column(name = "userName", unique = true)
private String userName;
@Column(name="password")
private String password;
public Doctor()
{
}
public Doctor(String firstName, String lastName)
{
super(firstName, lastName);
}
public Doctor(String firstName, String lastName, String gender, String address, String birthDate, String specialty, String degree)
{
super(firstName, lastName);
this.gender = gender;
this.address = address;
this.birthDate = birthDate;
this.specialty = specialty;
this.degree = degree;
}
public int getdId()
{
return dId;
}
public void setdId(int dId)
{
this.dId = dId;
}
public String getFirstName()
{
return firstName;
}
public void setFirstName(String firstName)
{
this.firstName = firstName;
}
public String getLastName()
{
return lastName;
}
public void setLastName(String lastName)
{
this.lastName = lastName;
}
public String getGender()
{
return gender;
}
public void setGender(String gender)
{
this.gender = gender;
}
public String getAddress()
{
return address;
}
public void setAddress(String address)
{
this.address = address;
}
public String getBirthDate()
{
return birthDate;
}
public void setBirthDate(String birthDate)
{
this.birthDate = birthDate;
}
public String getSpecialty()
{
return specialty;
}
public void setSpecialty(String specialty)
{
this.specialty = specialty;
}
public String getDegree()
{
return degree;
}
public void setDegree(String degree)
{
this.degree = degree;
}
public String getUserName()
{
return userName;
}
public void setUserName(String userName)
{
this.userName = userName;
}
public String getPassword()
{
return password;
}
public void setPassword(String password)
{
this.password = password;
}
}
接下来,这是我的 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/MedicalSystem</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop the existing tables and create new one -->
<property name="hbm2ddl.auto">create-drop</property>
<!-- Mention here all the model classes along with their package name -->
<mapping class="CS157B.Doctor"/>
<mapping class="CS157B.Patient"/>
<mapping class="CS157B.MedicalRecord"/>
<mapping class="CS157B.Specialty"/>
<mapping class="CS157B.Administrator"/>
<mapping class="CS157B.Staff"/>
</session-factory>
</hibernate-configuration>
这是我的病人类别
import javax.persistence.*;
@Entity
@Table(name="Patient")
@AttributeOverrides({
@AttributeOverride(name="firstname", column=@Column(name="firstName")),
@AttributeOverride(name="lastname", column=@Column(name="lastName")),
})
public class Patient extends Person
{
@Id
@GeneratedValue
@Column(name="pid")
private int pid;
@Column(name="firstName")
private String firstName;
@Column(name="lastName")
private String lastName;
@Column(name="gender")
private String gender;
@Column(name="birthDate")
private String birthDate;
@Column(name="address")
private String address;
@Column(name="seniorCitizen")
private boolean seniorCitizen;
@Column(name = "userName", unique = true)
private String userName;
@Column(name="password")
private String password;
@ManyToOne()
private Doctor doctor;
public Patient()
{
}
public Patient(String firstName, String lastName)
{
super(firstName, lastName);
}
public Patient(String firstName, String lastName, String gender, String address, String birthDate, boolean seniorCitizen)
{
super(firstName, lastName);
this.gender = gender;
this.address = address;
this.birthDate = birthDate;
this.seniorCitizen = seniorCitizen;
}
public int getPid()
{
return pid;
}
public void setPid(int pid)
{
this.pid = pid;
}
public String getFirstName()
{
return firstName;
}
public void setFirstName(String firstName)
{
this.firstName = firstName;
}
public String getLastName()
{
return lastName;
}
public void setLastName(String lastName)
{
this.lastName = lastName;
}
public String getGender()
{
return gender;
}
public void setGender(String gender)
{
this.gender = gender;
}
public String getBirthDate()
{
return birthDate;
}
public void setBirthDate(String birthDate)
{
this.birthDate = birthDate;
}
public String getAddress()
{
return address;
}
public void setAddress(String address)
{
this.address = address;
}
public Doctor getDoctor()
{
return doctor;
}
public void setDoctor(Doctor doctor)
{
this.doctor = doctor;
}
public boolean isSeniorCitizen()
{
return seniorCitizen;
}
public void setSeniorCitizen(boolean seniorCitizen)
{
this.seniorCitizen = seniorCitizen;
}
public String getUserName()
{
return userName;
}
public void setUserName(String userName)
{
this.userName = userName;
}
public String getPassword()
{
return password;
}
public void setPassword(String password)
{
this.password = password;
}
}
最后,这是我的 Connect 类:
import org.hibernate.*;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
public class Connect
{
private static SessionFactory sessionFactory;
private static ServiceRegistry serviceRegistry;
private static SessionFactory configureSessionFactory()
{
Configuration configuration = new Configuration().configure();
StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());
sessionFactory = configuration.buildSessionFactory(builder.build());
return sessionFactory;
}
public static SessionFactory getSessionFactory()
{
return configureSessionFactory();
}
}
我知道这有点长,但这是我在 Stack Overflow 上的第一篇文章。
最佳答案
尝试更新您的架构
更新
而不是
创建-删除
在使用 hbm2ddl.auto 的情况下,它是创建删除的,每次部署项目时它都会删除您的架构并创建新的架构。但就 hbm2ddl.auto 而言,每当您部署项目并将数据插入数据库时,它都会更新您的数据库,而不是删除和创建。
关于java - Hibernate:hbm2ddl 属性值,创建删除总是删除架构,即使没有显式 SessionFactory 关闭?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22088334/
是否可以简化在裸机上运行的这条链: 具有随时间变化的副本数的 StatefulSet 服务 使用 proxy-next-upstream: "error http_502 timeout invali
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
我需要为应用程序制定架构。它专为销售产品而设计。 系统每天将接受大约 30-40k 的新产品。它将导致在表 product 中创建新记录。 系统应保留价格历史记录。用户应该能够看到产品 A 的价格在去
我需要一些帮助来理解 PHP 的内部工作原理。 还记得,在过去,我们曾经写过 TSR(Terminate and stay resident)例程(pre-windows 时代)吗?一旦该程序被执行,
1.Nginx 基础架构 nginx 启动后以 daemon 形式在后台运行,后台进程包含一个 master 进程和多个 worker 进程。如下图所示: master与
本文深入探讨了Kubernetes(K8s)的关键方面,包括其架构、容器编排、网络与存储管理、安全与合规、高可用性、灾难恢复以及监控与日志系统。 关注【TechLeadCloud】,
我知道 CNN 的工作原理,包括每一层的用途(Dropout、Pooling 等)。但是,在为新数据集设计 CNN 时,我不知道要使用多少个 Conv-Relu-Pool 层,在最终获得输出之前我应该
在基于 REST 的架构中,资源和方法之间有什么区别。有吗? 最佳答案 资源是您的应用程序定义的东西;它们与物体非常相似。方法是 HTTP 动词之一,例如 GET、POST、PUT、DELETE。它们
我想用 oneOf仅在 xyType 的值上不同的模式属性(property)。我想要其中两个:一个是 xyType设置为 "1"第二个在哪里xyType是 任何其他值 .这可以使用 json 模式完
寻求 PHP 架构师的建议! 我对 PHP 不是很熟悉,但已经接管了一个用该语言编写的大型分析包的维护工作。该架构旨在将报告的数据读取到大型键/值数组中,这些数组通过各种解析模块传递,以提取每个模块已
这些存在吗? 多年来,我一直是大型强类型面向对象语言(Java 和 C#)的奴隶,并且是 Martin Fowler 及其同类的信徒。 Javascript,由于它的松散类型和函数性质,似乎不适合我习
我已经阅读了 Manning 的 Big Data Lambda Architecture ( http://www.manning.com/marz/BD_meap_ch01.pdf ),但仍然无法
在过去的几年里,我做了相当多的 iOS 开发,所以我非常熟悉 iOS 架构和应用程序设计(一切都是一个 ViewController,您可以将其推送、弹出或粘贴到选项卡栏中)。我最近开始探索正确的 M
我有以下应用程序,我在其中循环一些数据并显示它。 {{thing.title}} {{thing.description}}
昨天我和我的伙伴讨论了我正在开发的这个电子购物网站的架构。请注意,我为此使用 ASP.NET。他非常惊讶地发现我没有将添加到购物车的项目保留在 ArrayList 或其他通用列表中,而是使用 LINQ
我正在使用在 tridion 蓝图层次结构中处于较低位置的出版物。从蓝图中较高级别的出版物继承的一些内容和模式不适合我的出版物,并且永远不会被我的出版物使用。 我将跟进添加这些项目的内部团队,并尝试说
我目前已经在 Cassandra 中设计了一个架构,但我想知道是否有更好的方法来做事情。基本上,问题在于大多数(如果不是全部)读取都是动态的。我构建了一个分段系统作为应用程序服务,读取动态自定义查询(
我正在按照 documentation 中给出的 icingaweb UI v 2.0 布局执行在服务器上设置 icinga 的步骤。 。我成功进入设置页面,该页面要求您输入 token ,然后按照步
我必须保存来自不同社交媒体的用户的不同个人资料。例如用户可能有 1 个 Facebook 和 2 个 Twitter 个人资料。如果我保存每个配置文件它作为新文档插入不同的集合中,例如 faceboo
我的团队使用 Puppet 架构,该架构目前可在多个环境(流浪者、暂存、生产)中容纳单个应用程序。 我们现在想要扩展此设置的范围以支持其他应用程序。他们中的许多人将使用我们已经定义的现有模块的子集,而
我是一名优秀的程序员,十分优秀!