如何使用Hibernate从数据库中选择值?

我是休眠的新手,我使用MySQL数据库,有两个表serviceTypeDetails,validateConfig.In serviceTypeDetails,它有四种类型的服务和另一个表validateconfig包含31行关于每个servicetypeid,使用休眠我怎么能够选择数据来自validateConfig以及需要的映射关联和查询。

ServiceTypeDetails.java

package com.example.HibernateDataManager;
public class ServiceTypeDetails
{
     private Long Id;
     private String serviceTypeName;
    public ServiceTypeDetails()
    {
    }
    public void setId(long id)
    {
        this.Id=id;
    }
    public Long getId()
    {
        return Id;
    }
    public void setServiceTypeName(String serviceName)
    {
        this.serviceTypeName=serviceName;
    }
    public String getServiceTypeName()
    {
        return serviceTypeName;
    }
}

ValidateConfiguration.java

package com.example.HibernateDataManager;

public class ValidateConfiguration
{
    private Long Id;
    private String fieldName;
    private String modeValue;

    public ValidateConfiguration()
    {

    }

    public void setId(Long serviceID)
    {
        this.Id=serviceID;
    }

    public Long getId()
    {
        return Id;
    }


    public void setFieldName(String fieldName)
    {
        this.fieldName=fieldName;
    }

    public String getFieldName()
    {
        return fieldName;
    }


     public void setModeValue(String modevalue)
    {
        this.modeValue=modevalue;
    }

    public String getModeValue()
    {
        return modeValue;
    }

}

Main.java

package com.arosys.HibernateDataManager;
import java.util.Iterator;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class Main
{

    public static void main(String s[])
    { try{
        SessionFactory factory=new Configuration().configure().buildSessionFactory();
        Session session=factory.openSession();
        String sql_query= "from  ValidateConfiguration";
        Query query=session.createQuery(sql_query);
        System.out.println(query);
        for(Iterator it=query.iterate();it.hasNext();)
        {
           ValidateConfiguration std = ( ValidateConfiguration) it.next();
          System.out.println("serviceTypeID: " + std.getId());
          System.out.println("FieldName: " + std.getFieldName());
        // System.out.println("ModeValue: " + validateconfig.getModeValue());
       }

        session.close();
        }catch(Exception e){e.printStackTrace();}
    }
 }



<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>

在你的hibernate.cfg.xml中。

然后检查生成的SQL查询。

数据库设计更多地出现了这个问题。

链接地址: http://www.djcxy.com/p/48829.html

上一篇: how to select value from database using hibernate?

下一篇: Continue JTable data on another JTable beside it instead of scrolling