我有一个像
@Entity
@Table(name = "STUDENTCOURSE")
public class Trainee {
private String studentId;
private Date sessionDateTime;
....
@Id
@Column(name = "Student_Id")
public String getStudentId() {
return studentId;
}
public void setStudentId(String studentId) {
this.studentId = studentId;
}
@Column(name = "Session_Date_Time")
@Temporal(TemporalType.TIMESTAMP)
public Date getSessionDateTime() {
return sessionDateTime;
}
public void setSessionDateTime(Date sessionDateTime) {
this.sessionDateTime = sessionDateTime;
}
....
} //end of class Trainee
我正在从数据库中查询,它给了我类似的结果集
@SuppressWarnings("unchecked")
@Transactional(readOnly=true)
public List<Trainee> findTraineeFromLegacy(String courseId, String fromDate, String toDate) throws Exception {
Query query = em.createNativeQuery(FIND_TRAINEE_LEGACY, Trainee.class);
return query.setParameter("courseId", courseId)
.setParameter("fromDate", fromDate)
.setParameter("toDate", toDate)
.getResultList();
} //end of findTraineeFromLegacy()
我想问假设Student_Id
数据库中是否存在相同的内容。像Student_Id
数据库中存在相同值 2,2,2 的 3 个。那么在从数据库中获取记录时,Trainee 类是否会引发任何异常?
因为Student_Id
标有@id
注释并且Id
应该是唯一的。我只是获取记录。我不会将任何记录发送到数据库。
谢谢。
编辑 - - - - - - - - -
DROP TABLE IF EXISTS employee;
CREATE TABLE employee (
id INT ,
name VARCHAR(50),
salary BIGINT
);
INSERT INTO employee(id, name, salary) VALUES (1, 'Basit', 70000);
INSERT INTO employee(id, name, salary) VALUES (1, 'Masood', 65000);
INSERT INTO employee(id, name, salary) VALUES (1, 'Kazim', 35000);
员工实体:
@Entity
public class Employee {
@Id
private int id;
private String name;
private long salary;
public Employee() {
}
public Employee(int id) {
this.id = id;
}
//getter and setters
@Override
public String toString() {
return "id = " + id + " : name = " + name + " : salary = " + salary;
}
} //end of class Employee
雇员服务
@Lazy
@Service("employeeServices")
@Repository
@Transactional
public class EmployeeServicesImpl implements EmployeeServices {
@PersistenceContext(unitName="mySql_emf")
private EntityManager em;
@Override
public List<Employee> findAllEmployees() {
TypedQuery<Employee> query = em.createQuery("SELECT e FROM Employee e", Employee.class);
return query.getResultList();
//Query emQuery = em.createNativeQuery("SELECT * FROM employee", Employee.class);
//return emQuery.getResultList();
} //end of findAllEmployees();
} //end of end of class EmployeeServicesImpl
员工测试:
public static void main(String[] args) {
String springXmlFile = "classpath:spring/app-context-xml.xml";
GenericXmlApplicationContext springContext = SpringUtil.loadSpringContext(springXmlFile);
EmployeeServices employeeServices = springContext.getBean("employeeServices", EmployeeServices.class);
// find all employees
List<Employee> emps = employeeServices.findAllEmployees();
for (Employee e : emps){
System.out.println("Found employee: " + e);
}
} //end of main()
输出:
Hibernate: select employee0_.id as id1_0_, employee0_.name as name2_0_, employee0_.salary as salary3_0_ from Employee employee0_
Found employee: id = 1 : name = Basit : salary = 70000
Found employee: id = 1 : name = Basit : salary = 70000
Found employee: id = 1 : name = Basit : salary = 70000
JDBC
@Lazy
@Service("jdbcManager")
@Repository
@Transactional
public class JdbcDatabaseManager {
public List<Employee> findAllEmployees() {
List<Employee> result = new ArrayList<Employee>();
String query = "SELECT * FROM employee";
Connection connection = null;
try {
connection = getMySqlConnection();
PreparedStatement statement = connection.prepareStatement(query);
ResultSet resultSet = statement.executeQuery();
while (resultSet.next()) {
Employee employee = new Employee();
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
Long salary = resultSet.getLong("salary");
employee.setId(id);
employee.setName(name);;
employee.setSalary(salary);;
result.add(employee);
}
} catch (SQLException ex) {
String errorMessage = ex.getMessage();
ex.printStackTrace();
} finally {
closeConnection(connection);
}
return result;
} //end of findAllEmployees()
} //end of class JdbcDatabaseManager
主要的
public static void main(String[] args) {
...
// find all employees
List<Employee> emps = employeeServices.findAllEmployees();
for (Employee e : emps){
System.out.println("Found employee: " + e);
}
System.out.println("JDBC");
System.out.println();
JdbcDatabaseManager jdbcManager = springContext.getBean("jdbcManager", JdbcDatabaseManager.class);
List<Employee> jdbcEmps = jdbcManager.findAllEmployees();
for (Employee e : jdbcEmps){
System.out.println("Found employee: " + e);
}
输出:
Hibernate: SELECT * FROM employee
Found employee: id = 1 : name = Basit : salary = 70000
Found employee: id = 1 : name = Basit : salary = 70000
Found employee: id = 1 : name = Basit : salary = 70000
JDBC
Found employee: id = 1 : name = Basit : salary = 70000
Found employee: id = 1 : name = Masood : salary = 65000
Found employee: id = 1 : name = Kazim : salary = 35000
} //end of main