我使用HQL从数据库中获取数据我得到sql syntext异常请检查我的代码
Emp.java
@Entity
@Table(name="TBL_STD_EMP")
public class Emp
{
@Id
@GeneratedValue
@Column(name = "id")
private int id;
@Column(name = "first_name")
private String firstName;
@Column(name = "last_name")
private String lastName;
@Column(name="SALARY")
private int salary;
@Temporal(TemporalType.DATE)
private Date joinDate;
@ElementCollection
@JoinTable(name="EMP_ADDRESS" ,joinColumns=@JoinColumn(name="USER_ID"))
java.util.Collection <Address> address= new java.util.ArrayList<Address>();
//setter and geeter methods
} 我的可嵌入课程
@Embeddable
public class Address {
@Column(name="DOOR_NO")
int doorNO;
public int getDoorNO() {
return doorNO;
}
public void setDoorNO(int doorNO) {
this.doorNO = doorNO;
}
public String getStreetName() {
return streetName;
}
public void setStreetName(String streetName) {
this.streetName = streetName;
}
public String getCountryName() {
return countryName;
}
public void setCountryName(String countryName) {
this.countryName = countryName;
}
@Column(name="STREET_NAME")
String streetName;
@Column(name="COUNTY_NAME")
String countryName;
} 我的主要班级 包com.hib.busness;
import java.util.Date;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import com.hib.dto.Address;
import com.hib.dto.EMP;
public class MainClass {
/**
* @param args
*/
private static SessionFactory factory;
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
factory = new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
System.err.println("Failed to create sessionFactory object." + ex);
throw new ExceptionInInitializerError(ex);
}
Transaction tx = null;
Integer employeeID = null;
Session session = factory.openSession();
try {
tx = session.beginTransaction();
EMP employee = new EMP();
employee.setFirstName("bhanu1");
employee.setLastName("prasad");
employee.setSalary(30000);
employee.setJoinDate(new Date());
employee.setDescription("Working as ProductEngineer");
Address address = new Address();
address.setCountryName("India");
address.setDoorNO(1);
address.setStreetName("mainStreet");
Address address1 = new Address();
address1.setCountryName("India1");
address1.setDoorNO(2);
address1.setStreetName("mainStreet1");
employee.getAddress().add(address);
employee.getAddress().add(address1);
employeeID = (Integer) session.save(employee);
tx.commit();
tx = session.beginTransaction();
String hql = "FROM EMP where lastName='prasad' and doorNO=2";
Query query = session.createQuery(hql);
List results = query.list();
System.out.println(results);
} catch (HibernateException e) {
if (tx != null)
tx.rollback();
e.printStackTrace();
} finally {
session.close();
}
}
}
我得到SQLSyntex异常,任何人都帮助我。
答案 0 :(得分:1)
在HQL
中,您可以使用点表示法导航实体的字段。建立Emp
和Address
之间的关联后,您可以在HQL语句中使用它。由于doorNO
字段位于Address
实体上,您应通过address
访问该字段。
尝试:
String hql = "FROM EMP e where e.lastName='prasad' and e.address.doorNO=2";
答案 1 :(得分:0)
我找到了上述问题的解决方案:
FROM EMP e where e.lastName='prasad' and DOOR_NO=1