Hello everyone, this is my first post on this forum. I'm actually facing a really strange behavior with my application, and I've lost the last two days in unsuccessful tries to come out from it. Here's a brief description of the model and algorithm:
I've got several network hosts (routers), I query them via SNMP with a library which I wrote, and then I have to the store the query results into a database.
Both targets to be queried and query results are stored into a Mysql database via Hibernate using JPA annotations. So the algorithm does something like this:
- Select from the database the NetworkTargets to be checked (this is actually working)
- Query them (it works)
- Stock CheckpointResults via Hibernate (works with a strange behavior, I'll explain it later).
Actually, a NetworkTarget has several fields and a List of String which models the network protocols that my NetworkTarget offers . I use JPA annotations to map objects:
Code:
@Entity
@Table(name="NetworkTargets")
public class NetworkTarget implements Serializable{
private String ip;
private String routerName;
private String snmpVersion;
private List<String> services;
private Set<String> flags;
private Long id;
...
@ElementCollection(fetch=FetchType.EAGER)
@JoinTable(name="NetworkTargetProtocols",joinColumns=@JoinColumn(name="RelatedHostID"))
@Cascade(CascadeType.PERSIST)
public List<String> getServices() {
return services;
}
//setter method omitted
...
The NetworkTargets are loaded properly from the database, and I'm able to query them flawlessy.
As you can see, I've got a JoinTable to keep track of the protocols that a NetworkTarget offers.
As soon as the query ends I save some CheckpointResults in my database. CheckpointResult class is made like this:
Code:
@Entity
@Table(name="CheckpointResults")
public class CheckpointResult implements Serializable{
private Long id;
private String ip;
private String startTimestamp;
private List<String> hostProtocols;
private List<CheckpointResultMib> protocolMibs;
private List<CheckpointResultMib> hostMibs;
...//related JPA annotations
Now the issue which I find absurd: when I save a CheckpointResult (with a session.save() ) I see in the database schema that all entries from the JoinTable mentioned before are deleted. I've activated the show_sql in my hibernate.cfg.xml, and I noticed that Hibernate issues a delete command on that table, and I sincerely don't know why this happens. Here's a bunch of the sql log I can see:
Quote:
//In this part I fetch my targets
Hibernate: select networktar0_.Id as Id0...
...
2 Target(s) found
//I start saving a CheckpointResult...
Hibernate: insert into JarvisHibernate.CheckpointResults (IP, CheckpointTime) values (?, ?)
Hibernate: insert into JarvisHibernate.RequestedMib (Name, isPointValue) values (?, ?)
Hibernate: insert into JarvisHibernate.MappedMib values ( )
//but then...
Hibernate: delete from JarvisHibernate.NetworkTargetProtocols where RelatedHostID=?
Hibernate: insert into JarvisHibernate.CheckpointedHostMibs (RelatedHost, RequestedHostMibID) values (?, ?)
Hibernate: insert into JarvisHibernate.CheckpointResult_protocols (CheckpointResult_Id, protocols) values (?, ?)
[other successful inserts]
CheckpointResult saved correctly
I was really astonished when I saw it, and I really have no idea about where this delete command can come from. The method which I use to store CheckpointResults is really simple:
Code:
public void createAndStoreCheckpointResult(CheckpointResult result) {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
session.save(result);
session.getTransaction().commit();
System.out.println("CheckpointResult saved correctly");
}
This is a long post, so I'm not gonna make it longer by posting other files, but if there's something that might come useful (such as full JPA annotations, hibernate.cfg.xml and so on), I'll post it as soon as I can. Any suggestions or ideas will be appreciated.
Thanks in advance for any response,
Luca