2

I have a two entities and relation between them OneToOne:

@Entity
@Getter
@Setter
public class Master {
    @Id
    private String id;

    private String name;

    @OneToOne(fetch = FetchType.LAZY, mappedBy = "master", optional = false)
    private Slave slave;
}

@Entity
@Getter
@Setter
public class Slave {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    @Column(name = "id_slave")
    private Integer id;

    @OneToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "id_master")
    @PrimaryKeyJoinColumn
    private Master master;
}

  MAIN:
  Master master = em.find(Master.class, "TEST_ID");

When I run this code I get

java.lang.IllegalArgumentException: Provided id of the wrong type for class entity.Slave. Expected: class java.lang.Integer, got class java.lang.String

I see that is due to @PrimaryKeyJoinColumn exists, who knows how to prevent this exception and if it is possible to point out type for PrimaryKeyJoinColumn. I can not remove @PrimaryKeyJoinColumn cause I want to have laziness.

idmitriev
  • 3,417
  • 3
  • 21
  • 38
  • What's the relation between laziness and the @PrimaryKeyJoinColumn annotation? Can you add your tables? I think that you are using that annotation in a wrong way. In "Java Persistence with Hibernate", that annotation is used when the primary key of Slave is also a foreign key that points to primary key on Master. That not seems to be your case. – RubioRic Jun 08 '16 at 17:50
  • One master has sole slave. Do you know that if you do not put @PrimaryKeyJoinColumn and optional=false that lazy does not work?? – idmitriev Jun 08 '16 at 19:20
  • No, I didn't know that fact. Can you provide some link for additional info on that matter? I have found nothing on JPA javadocs that states such thing. And the same occurs in the book that I mentioned. You have not shown your table structure. How are those Masters and Slaves related in the DB? – RubioRic Jun 09 '16 at 04:54
  • OK. You can read about this right here - http://stackoverflow.com/questions/17987638/hibernate-one-to-one-lazy-loading-optional-false. FK on master is in slave table. – idmitriev Jun 09 '16 at 05:02
  • Thanks for the link. I'm not discussing the benefits of using "optional=false" on Master. Read again my first comment. Why do you need @PrimaryKeyJoinColumn? I think that "that annotation is used when the primary key of Slave is also a foreign key that points to primary key on Master". Is that your situation? Have you tried removing that annotation? – RubioRic Jun 09 '16 at 05:11
  • BTW that's a 2013 post. Maybe that issue has been fixed in more recent versions of Hibernate. I don't know that. Do you? – RubioRic Jun 09 '16 at 05:17
  • It has not been fixed yet. You must use both @PrimaryKeyJoinColumn and optional=false, you should check it, otherwise you can not feel this pain. – idmitriev Jun 09 '16 at 07:45
  • Ok. I'll try to test it later. Maybe you're right. +1 to your question. Hopefully someone else can help us to feel no pain. ;-P – RubioRic Jun 09 '16 at 08:56

0 Answers0