我有一个像
这样的课程@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
。像3 Student_Id
相同的值2,2,2存在于数据库中。然后在从数据库中获取记录时,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
EmployeyServicess
@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