how to select value from database using hibernate?
I am newbie for hibernate,i am using mysql database,with two tables serviceTypeDetails,validateConfig.In serviceTypeDetails,it's having four types of services and another table validateconfig contain 31 row with respect to each servicetypeid,using hibernate how i am able to select data from validateConfig and what are required mapping association and query for it.
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() { retu开发者_运维百科rn 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();} } }
Set
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
in your hibernate.cfg.xml.
Then check what SQL query is getting generated.
The problem more seems towards Database Design.
精彩评论