I have the following dilemma to generate a list of all the credentials that belong to a specific physical host.
Class FisicHost
@Entity
@Transactional
public class FisicHost {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ManyToOne(fetch=FetchType.EAGER)
private Datacenter datacenter;
@OneToMany(mappedBy = "fisicHost")
@LazyCollection(LazyCollectionOption.FALSE)
private List<Credential> credentials;
private String name;
private String ip;
private String operatingSystem;
private String notes;
public FisicHost(){
}
public FisicHost(Long id, Datacenter datacenter, List<Credential> credentials, String name, String ip, String operatingSystem, String notes) {
this.id = id;
this.datacenter = datacenter;
this.credentials = credentials;
this.name = name;
this.ip = ip;
this.operatingSystem = operatingSystem;
this.notes = notes;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Datacenter getDatacenter() {
return datacenter;
}
public void setDatacenter(Datacenter datacenter) {
this.datacenter = datacenter;
}
public List<Credential> getCredentials() {
return credentials;
}
public void setCredentials(List<Credential> credentials) {
this.credentials = credentials;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public String getOperatingSystem() {
return operatingSystem;
}
public void setOperatingSystem(String operatingSystem) {
this.operatingSystem = operatingSystem;
}
public String getNotes() {
return notes;
}
public void setNotes(String notes) {
this.notes = notes;
}
}
Class Credential
@Entity
public class Credential {
@Id
private int id;
@ManyToOne(fetch= FetchType.EAGER)
private FisicHost fisicHost;
private String user;
private String password;
private String notes;
private String role;
public Credential(){
}
public Credential(int id, FisicHost fisicHost, String user, String password, String notes, String role) {
this.id = id;
this.fisicHost = fisicHost;
this.user = user;
this.password = password;
this.notes = notes;
this.role = role;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public FisicHost getFisicHost() {
return fisicHost;
}
public void setFisicHost(FisicHost fisicHost) {
this.fisicHost = fisicHost;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getNotes() {
return notes;
}
public void setNotes(String notes) {
this.notes = notes;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
}
And in this Repository method I'm trying to do it that way, but it seems to me that here is the error:
@Repository
public class CredentialDaoImpl implements CredentialDao {
@Autowired
private SessionFactory sessionFactory;
@Override
public List<Credential> getAllCredentialsByFisicHost(FisicHost fisicHost) {
// Open a session
Session session = sessionFactory.openSession();
Criteria c = session.createCriteria(Credential.class).add(Restrictions.eq("fisicHost", fisicHost.getId()));
List<Credential> allCredentials = c.list();
// Close the session
session.close();
return allCredentials;
}
And the call from the controller occurs like this:
@RequestMapping(value = "/chosenDatacenter", method = RequestMethod.POST)
public String datacenterPostHandler(@RequestParam("datacenterList") String name, ModelMap modelMap){
List<Datacenter> allDatacenters = datacenterDao.getAllDatacenters();
for (Datacenter dc : allDatacenters) {
if (dc.getName().equals(name)) {
modelMap.put("datacenter", dc);
if(dc.getFisicHostList().size() != 0) {
List<FisicHost> datacenterFisicHosts = dc.getFisicHostList();
modelMap.put("datacenterFisicHosts", datacenterFisicHosts);
for(FisicHost fh : datacenterFisicHosts){
if(fh.getCredentials().size() != 0){
modelMap.put("fisicHostCredentialsList", credentialDao.getAllCredentialsByFisicHost(fh));
}
}
}
return "chosenDatacenter";
}
}
return null;
}
and I miss this error:
There was an unexpected error (type = Internal Server Error, status = 500). Error accessing field [private java.lang.Long com.amco.Datacenter.model.FisicHost.id] by reflection for persistent property [com.amco.Datacenter.model.FisicHost # id]: 20; nested exception is org.hibernate.property.access.spi.PropertyAccessException: Error accessing field [private java.lang.Long com.amco.Datacenter.model.FisicHost.id] by reflection for persistent property [com.amco.Datacenter.model.FisicHost # id]: 20
I'm sure the problem is in this line of the CredentialDaoImpl class
Criteria c = session.createCriteria(Credential.class).add(Restrictions.eq("fisicHost", fisicHost.getId()));
since the ID # 20 in my database corresponds to a FisicHost that is the one I use to test, but I do not know how to solve the problem.
How can I make the search criteria be by ID when the ID I'm looking for is from an object of another class?
Basically I'm doing all this to get the credentials within each FisicHost and deploy them in the corresponding template.