0

I'm trying to get Emails entity in one of my DAO's method by this method:

Emails mail = entityManager.find(Emails.class, email);

//test
System.out.println("code: " + code + ", email: " + email);
System.out.println(mail == null ? "mail equal null"
    : ("mail:" + mail.getConfirmationCode() + ", "
            + mail.getEmail() + ", " + mail.getUser_id()));

where entityManager's find method returns null. I'm sure that both arguments of that method are proper and that row exists in my database so the method shuld return an entity, not null.

And what's important. When i'm using the same method in another method of the same DAO class with the same arguments then it works fine and entityManager's find method returns proper entity. All the methods are annotated as Transactional.

I note that when i call find method referencing to another row then it returns proper entity. The problem is only with one entity, but the same find mathod with that entity still works in anothers DAO's method.

Update: Let's say the primary key of that row is "adrian@domain.com". Then (All in the same DAO's method) when i type the PK in the second argument manually then it returns proper Entity:

Emails mailAddresse = entityManager.find(Emails.class, "adrian@domain.com");

and when i pass email instance to the method's PK argument which is "adrian@domain.com", then it returns null:

Emails mail = entityManager.find(Emails.class, email);
        System.out.println("email: " + email);

In the console i can see email: "adrian@domain.com" BUT following code print in console email not equal adrian@domain.com

if("adrian@domain.com".equals(email)) System.out.println("email equal adrian@domain.com");
        else System.out.println("email not equal adrian@domain.com");

When i would expect email equal adrian@domain.com. Do anybody know why?

Problem solved: The reason that find method was returning null was that i was passing an email to email variable by assuming a HTTP request parameter with double quotes, which is wrong way:

.../confirmation?email="adrian@domain.com"

and should be without any quotes, that way:

.../confirmation?email=adrian@domain.com
Kavv
  • 209
  • 3
  • 13
  • The `find` method expects the type `Emails` and a primary key, so if the value you are putting in is a value of the primary key it works, if it is anything else but a primary key it won't work. – M. Deinum Sep 30 '15 at 06:59
  • I'm putting primary key as a second argument. – Kavv Sep 30 '15 at 07:04
  • I note that when i call find method referencing to another row then it returns proper entity. The problem is only with one entity, but the same find mathod with that entity still works in anothers DAO's method. – Kavv Sep 30 '15 at 07:06
  • email instance is what type? Is it a primary key? – Prakash Bhagat Sep 30 '15 at 07:07
  • email is a String type and it's primary key. I sad when calling find method with emails referencing to another row, then it works – Kavv Sep 30 '15 at 07:11
  • Then it simply doesn't match, make sure no trailing spaces are in there, weird chars etc. – M. Deinum Sep 30 '15 at 07:13
  • First post updated. Look, please. @M. Deinum – Kavv Sep 30 '15 at 07:32
  • Do `System.out.println("Email: ["+email+"]");` and check for trailing spaces. – M. Deinum Sep 30 '15 at 07:38
  • it prints *Email: ["adrian@domain.com"]* – Kavv Sep 30 '15 at 07:45
  • There must be something different in your database, else it will match... Again check for whitespace, strange chars, character encodings etc. – M. Deinum Sep 30 '15 at 07:52
  • Everything is ok in my database. The problem is that the email instance is not equal to PK, but when i print email instance then seems to be the same as a primary key but it isn't. – Kavv Sep 30 '15 at 07:55
  • one moment i will check encoding. It could be the problem. You right – Kavv Sep 30 '15 at 08:01

0 Answers0