im目前正在使用WebServices。我有一个正常工作的REST服务。它适用于简单的POJO列表,并公开此列表的CRUD操作。它通过InelliJ生成的工件(战争)部署在Glassfish上。现在,我想通过Hibernate将其连接到mySQL数据库。我创建了一个简单的测试项目,并且能够使用Hibernate映射对数据库进行更改。 我的POJO:
@Entity
@Table(name = "user_table")
public class User {
@Id
@Column(name = "user_id")
private int userid;
@Column(name = "user_name")
private String username;
@Column(name = "created_by")
private String createdBy;
@Column(name = "created_date")
private Date createdDate;
public int getUserid() {
return userid;
}
public void setUserid(int userid) {
this.userid = userid;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getCreatedBy() {
return createdBy;
}
public void setCreatedBy(String createdBy) {
this.createdBy = createdBy;
}
public Date getCreatedDate() {
return createdDate;
}
public void setCreatedDate(Date createdDate) {
this.createdDate = createdDate;
}
}
连接到数据库的类。
package com.jcg.hibernate.maven;
import java.util.Date;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class AppMain {
static User userObj;
static Session sessionObj;
static SessionFactory sessionFactoryObj;
private static SessionFactory buildSessionFactory() {
return new Configuration().configure().buildSessionFactory();
}
public static void main(String[] args) {
System.out.println(".......Hibernate Maven Example.......\n");
try {
sessionObj = buildSessionFactory().openSession();
sessionObj.beginTransaction();
for(int i = 101; i <= 105; i++) {
userObj = new User();
userObj.setUserid(i);
userObj.setUsername("Editor " + i);
userObj.setCreatedBy("Administrator");
userObj.setCreatedDate(new Date());
sessionObj.save(userObj);
}
System.out.println("\n.......Records Saved Successfully To The Database.......\n");
sessionObj.getTransaction().commit();
} catch(Exception sqlException) {
if(null != sessionObj.getTransaction()) {
System.out.println("\n.......Transaction Is Being Rolled Back.......");
sessionObj.getTransaction().rollback();
}
sqlException.printStackTrace();
} finally {
if(sessionObj != null) {
sessionObj.close();
}
}
}
}
它在本地运行良好(数据库和服务器都在我的本地计算机上),但是我不知道如何将数据库与服务合并。我也应该将数据库部署到服务器吗?对于正确方向的任何提示,我们将不胜感激。