-1

Below is my Pojos

@Entity
@Table(name = "XXXXXXXX")
public class MyDTO1 implements Serializable{

@Embeded
private MyDTO2 myDTO2;
private Integer int1;
}

@Embedable
public class MyDTO2 implements Serializable{

private String string1;
}

Actual Class:

pulic class Test(){

private void testMethod(){
Session session = getSession();
List<ClientProgramINDTO> clientProgramDto = session.createCriteria(ClientProgramINDTO.Class)
.add(Restrictions.eq("int1", 1))
.add(Restrictions.eq("myDTO2.string1", "Test")).list();
}


}

I am getting below error message:

could not resolve property myDTO2.string1  in MyDto1.
Chaitanya
  • 15,403
  • 35
  • 96
  • 137

2 Answers2

0

Workaround is to use metamodel.

I hope this solves your problem. Have a look and detailed explanantion.

Community
  • 1
  • 1
Ankur Singhal
  • 26,012
  • 16
  • 82
  • 116
0

There are some mistakes in the your code:

@Embeded should be @Embedded in entity MyDTO1

@Embedable should be @Embeddable in entity MyDTO2

pulic class Test(){ should be public class Test{

ClientProgramINDTO.Class should be ClientProgramINDTO.class

So without fixing these compilation issues you cannot run the program.

Also, you have not provided what ClientProgramINDTO object is and how is it related to other entities, I am assuming that ClientProgramINDTO is same as MyDTO1.

If I proceed based on my assumption then there is no issue in the code. Here is the sample code that I have tried:

public class Test {
    public static void main(String[] args) {
        saveData();
        testMethod();
        HibernateUtil.getSessionFactory().close();
    }
    private static void saveData() {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.getTransaction().begin();

        ClientProgramINDTO c = new ClientProgramINDTO();
        c.setInt1(1);
        c.setMyDTO2(new MyDTO2("Test"));
        session.save(c);

        session.getTransaction().commit();
    }
    private static void testMethod() {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.getTransaction().begin();
        List<ClientProgramINDTO> clientProgramDto = session
                .createCriteria(ClientProgramINDTO.class)
                .add(Restrictions.eq("int1", 1))
                .add(Restrictions.eq("myDTO2.string1", "Test")).list();
        for (ClientProgramINDTO clientProgramINDTO : clientProgramDto) {
            System.out.println(clientProgramINDTO.getInt1() + " "+ clientProgramINDTO.getMyDTO2().getString1());

        }
        session.getTransaction().commit();      
    }
}

Output of this program:

1 Test

Chaitanya
  • 15,403
  • 35
  • 96
  • 137