我是Hibernate的新手,但是我需要将它用于工作中的项目。我们将Netbeans与Derby Embedded一起使用,我们无法更改为其他IDE。
我遇到的问题与数据库中的多对多关系有关。
我有一个表PROBLEM,一个表MACHINE和一个连接表MACHINEPROBLEM。问题可以有很多机器,机器可能会有很多问题。这些表通过联结表MACHINEPROBLEM相关联。
我有必要的POJO和映射文件(在hibernate.cfg.xml中适当引用),让Netbeans和Hibernate自动在数据库中创建表。 表格和各自的关系是正确创建的。表MACHINEPROBLEM有一个复合键,由“machineid”组成,它在MACHINE表中引用“id”,而“problemid”在PROBLEM表中引用“id”。
当我创建一个Problem对象时,向它添加一套机器,然后将它保存在数据库中,联结表没有填充任何数据。数据在PROBLEM和MACHINE表中正确保存,但MACHINEPROBLEM表为空。这意味着我无法搜索给定问题的所有机器,因为用于连接它们的联结表是空的。
任何人都可以帮我解决这个问题吗?谢谢。
映射文件:
Machine.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="DataAccess.entity.Machine" table="MACHINE" schema="APP">
<id name="id" type="long">
<column name="ID" />
<generator class="assigned" />
</id>
<set name="problems" inverse="false" lazy="true" fetch="select" table="MACHINEPROBLEM">
<key column="MACHINEID"/>
<many-to-many column="PROBLEMID" class="DataAccess.entity.Problem"/>
</set>
<property name="machineid" type="string">
<column name="MACHINEID" length="10" not-null="true" />
</property>
</class>
</hibernate-mapping>
Problem.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="DataAccess.entity.Problem" table="PROBLEM" schema="APP">
<id name="id" type="long">
<column name="ID" />
<generator class="assigned" />
</id>
<set name="machines" inverse="true" lazy="true" fetch="select" cascade="all" table="MACHINEPROBLEM">
<key column="PROBLEMID"/>
<many-to-many column="MACHINEID" class="DataAccess.entity.Machine"/>
</set>
<property name="problemid" type="string">
<column name="PROBLEMID" length="10" not-null="true" />
</property>
<property name="problemname" type="string">
<column name="PROBLEMNAME" length="50" not-null="true" />
</property>
<property name="tipoproblema" type="string">
<column name="TIPOPROBLEMA" length="50" />
</property>
<property name="linear" type="boolean">
<column name="LINEAR" not-null="true" />
</property>
<property name="numtarefas" type="int">
<column name="NUMTAREFAS" not-null="true" />
</property>
<property name="nummaquinas" type="int">
<column name="NUMMAQUINAS" not-null="true" />
</property>
</class>
</hibernate-mapping>
的的POJO:
Machine.java
public class Machine implements java.io.Serializable {
private long id;
private String machineid;
private Set<Problem> problems = new HashSet<Problem>();
public Machine() {
}
public Machine(long id, String machineid) {
this.id = id;
this.machineid = machineid;
}
public long getId() {
return this.id;
}
public void setId(long id) {
this.id = id;
}
public String getMachineid() {
return this.machineid;
}
public void setMachineid(String machineid) {
this.machineid = machineid;
}
public Set<Problem> getProblems() {
return problems;
}
public void setProblems( Set<Problem> problems ) {
this.problems = problems;
}
public boolean equals(Object obj) {
if (obj == null) return false;
if (!this.getClass().equals(obj.getClass())) return false;
Machine obj2 = (Machine)obj;
if((this.id == obj2.getId()) && (this.machineid.equals(obj2.getMachineid())))
{
return true;
}
return false;
}
public int hashCode() {
int tmp = 0;
tmp = ( id + machineid ).hashCode();
return tmp;
}
}
Problem.java
public class Problem implements java.io.Serializable {
private long id;
private String problemid;
private String problemname;
private String tipoproblema;
private boolean linear;
private int numtarefas;
private int nummaquinas;
private Set<Machine> machines = new HashSet<Machine>();
public Problem() {
}
public Problem(long id, String problemid, String problemname, boolean linear, int numtarefas, int nummaquinas) {
this.id = id;
this.problemid = problemid;
this.problemname = problemname;
this.linear = linear;
this.numtarefas = numtarefas;
this.nummaquinas = nummaquinas;
}
public Problem(long id, String problemid, String problemname, String tipoproblema, boolean linear, int numtarefas, int nummaquinas) {
this.id = id;
this.problemid = problemid;
this.problemname = problemname;
this.tipoproblema = tipoproblema;
this.linear = linear;
this.numtarefas = numtarefas;
this.nummaquinas = nummaquinas;
}
public long getId() {
return this.id;
}
public void setId(long id) {
this.id = id;
}
public String getProblemid() {
return this.problemid;
}
public void setProblemid(String problemid) {
this.problemid = problemid;
}
public String getProblemname() {
return this.problemname;
}
public void setProblemname(String problemname) {
this.problemname = problemname;
}
public String getTipoproblema() {
return this.tipoproblema;
}
public void setTipoproblema(String tipoproblema) {
this.tipoproblema = tipoproblema;
}
public boolean isLinear() {
return this.linear;
}
public void setLinear(boolean linear) {
this.linear = linear;
}
public int getNumtarefas() {
return this.numtarefas;
}
public void setNumtarefas(int numtarefas) {
this.numtarefas = numtarefas;
}
public int getNummaquinas() {
return this.nummaquinas;
}
public void setNummaquinas(int nummaquinas) {
this.nummaquinas = nummaquinas;
}
public Set<Machine> getMachines() {
return machines;
}
public void setMachines( Set<Machine> machines ) {
this.machines = machines;
}
}
主要课程:
public class Test {
public static void main(String[] args) {
try
{
//Adds a couple of machines to a hashset
Set<Machine> machines = new HashSet<>();
machines.add(new Machine(1, "M1"));
machines.add(new Machine(2, "M2"));
//creates a Problem with some random data
Problem p = new Problem(2, "OSD1", "TestP", "jobshop", true, 2, 3);
p.setMachines(machines);
//Adds Problem p to the database
ProblemDAL pDal = new ProblemDAL(null);
pDal.add(p); //Doing this saves Problem p to table PROBLEM, as well as the set of machines to table MACHINE. However, nothing's saved in the junction table MACHINEPROBLEM.
pDal.closeSession();
}
catch (Exception e){
e.printStackTrace();
}
}
}
的 ProblemDAL
public class ProblemDAL {
Session session;
boolean closeSession;
public ProblemDAL(Session session)
{
if(session == null)
{
this.session = HibernateUtil.getSessionFactory().openSession();
}
else
{
this.session = session;
}
}
public void add(Problem entity) throws Exception {
Transaction tx = null;
try {
tx = session.beginTransaction();
session.save(entity);
tx.commit();
}
catch (HibernateException e) {
if (tx!=null) tx.rollback();
e.printStackTrace();
}
}
public void update(Problem entity)
{
Transaction tx = null;
try {
tx = session.beginTransaction();
session.update(entity);
tx.commit();
}
catch (HibernateException e) {
if (tx!=null) tx.rollback();
e.printStackTrace();
}
}
public void delete(Problem entity){
Transaction tx = null;
try{
tx = session.beginTransaction();
session.delete(entity);
tx.commit();
}
catch (HibernateException e) {
if (tx!=null) tx.rollback();
e.printStackTrace();
}
}
public Problem getById(long id) {
Transaction tx = null;
Problem entity = null;
try {
tx = session.beginTransaction();
entity = (Problem) session.get(Problem.class, id);
tx.commit();
}
catch (HibernateException e) {
if (tx!=null) tx.rollback();
e.printStackTrace();
}
finally {
return entity;
}
}
public List<Problem> getAll() {
Transaction tx = null;
List<Problem> entityList = null;
try {
tx = session.beginTransaction();
entityList = session.createQuery("from Problem").list();
tx.commit();
}
catch (HibernateException e) {
if (tx!=null) tx.rollback();
e.printStackTrace();
}
finally {
return entityList;
}
}
public List<Machine> getMachines() {
Transaction tx = null;
List<Machine> entityList = null;
try {
tx = session.beginTransaction();
entityList = session.createQuery("select machines from Problem").list();
tx.commit();
}
catch (HibernateException e) {
if (tx!=null) tx.rollback();
e.printStackTrace();
}
finally {
return entityList;
}
}
public void closeSession()
{
if(this.session!=null)
{
this.session.close();
}
}
}
的 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>
<property name="hibernate.dialect">org.hibernate.dialect.DerbyDialect</property>
<property name="hibernate.connection.driver_class">org.apache.derby.jdbc.EmbeddedDriver</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.connection.url">jdbc:derby:DB;create=true</property>
<property name="hibernate.connection.username">***</property>
<property name="hibernate.connection.password">***</property>
<property name="hibernate.show_sql">true</property>
<mapping resource="DataAccess/entity/Problem.hbm.xml"/>
<mapping resource="DataAccess/entity/Machine.hbm.xml"/>
<mapping resource="DataAccess/entity/Operation.hbm.xml"/>
<mapping resource="DataAccess/entity/EvaluationParameters.hbm.xml"/>
<mapping resource="DataAccess/entity/Generatedjobs.hbm.xml"/>
<mapping resource="DataAccess/entity/Jobs.hbm.xml"/>
</session-factory>
</hibernate-configuration>
答案 0 :(得分:1)
此类问题最常见的问题是您没有设置关系的双方。
此时无视任何Hibernate问题,无论如何都是最好的。
如果你考虑:
Problem p = new Problem();
Machine m = new Machine();
m.addProblem(p);
忽略任何持久性问题,您的域模型是否处于合理状态,即。机器'm'与问题'p'有关吗?即p.getMachines()。contains(m)返回true?
所以通常最好封装这些操作:
public class Machine{
public void addProblem(Problem p){
problems.add(p);
p.getMachines.add(this);
}
public class Problem{
public void addMachine(Machine m){
machines.add(m);
m.getProblems().add(this);
}
作为实验,您可以尝试使用当前代码来保存具有一组问题的新计算机。结果应与预期的一样,因为机器是拥有(非反向)方。