This problem turned out to be quite difficult for me. There is Hibernate. It used to be written in such a way that the record was inserted into the TableView and it was immediately highlighted. Now I do differently. I add it immediately to the database, then I launch the view from the TableView, at this time the entire list of records is read and inserted into the TableView. After that, you need to highlight the one that I added. Simply do this:

groupTypeDao.save(groupType); .... // запускается фрейм .... List<GroupType> groupTypes = groupTypeDao.findAll(); ObservableList<GroupType> ol = FXCollections.observableArrayList(groupTypes); tableView.setItems(ol); // подсветка tableView.getSelectionModel().select(groupType); 

And nothing is highlighted. I tried to find out the line number in tableView and highlight by number.

 Integer number = tableView.getItems().indexOf(groupType); 

In the end, number = -1. Those. An object in the list of tableView objects was not found. But he is there. Digging further into the debugger gave only that for some reason two identical objects from DAO come out as different (with different hashes). Those. if done

 GroupType groupType = groupTypeDao.findById(3); GroupType groupType1 = groupTypeDao.findById(3); 

And then

 groupType.equals(groupType1) 

will return false. Also, objects have different hashCode (). Dig further. Inside the objects are no different, except for the links, which are tightened by LasyInitialization. Those. in both groupType there is a userAdded field that points to the same user (he is generally alone in the database), but the hashcodes of this connection are different again.

There is another project in which if I write also the choice of two identical objects from the database - they will be the same. With identical hash codes. Both projects have exactly the same configs, exactly the same versions of Hibernate and MySQL. I do not know where to dig. Tell me please.

PS: In more detail, with screenshots laid out here: http://www.cyberforum.ru/java-gui-javafx/thread1824661.html But there are also those who want (or who can) no help.

PPS: Minimum example:

GroupTypeDao

 @Repository @Transactional public class GroupTypeDaoImpl implements GroupTypeDao { @Autowired private SessionFactory sessionFactory; @Override public GroupType findById(Integer id) { return (GroupType) sessionFactory.getCurrentSession() .createQuery("from GroupType where id = :id") .setParameter("id", id).uniqueResult(); } @Override @SuppressWarnings("unchecked") public List<GroupType> findAll() { return sessionFactory.getCurrentSession().createQuery("from GroupType").list(); } } 

If we do this:

 List<GroupType> groups = groupTypeDao.findAll(); GroupType groupType = groups.get(3); int number = groups.indexOf(groupType); 

then the number will be 3. The third element of the list, everything is fine.

If we do this:

 List<GroupType> groups = groupTypeDao.findAll(); GroupType groupType = groupTypeDao.findById(3); int number = groups.indexOf(groupType); 

then the number will be -1. Those. The object in the list was not found.

Those. if you take an object from the database, it will not be found in the list from the same database. Yes, I agree that a possible solution is to override equals () and hashCode (). But in another project, the same operation with the same settings goes off with a bang. Without any overrides equals () and hashCode ().

PPS: solution found - override equals () and hashCode (). The reason that in another project works without redefining these methods is the hibbernay proxy.

  • From the question it is not clear how to help you and where to start. Make a minimal example that can be played. - MrFylypenko 2:41 pm
  • Added an example of a problem. - Virkom
  • Add more GroupType code. Well and a question on filling: if to redefine hashCode and equals, everything works? - MrFylypenko
  • This is Java, you need to understand that hibernate in groupTypeDao.findAll () and groupTypeDao.findById (3) returns different instances of the GroupType class, overriding equals () and hashCode () is necessary, no one will determine the equivalence of two different objects. - Ruslan P.
  • Thanks for the help, I have already explained why all this happens (added to the end of the question). - Virkom

0