Quantcast
Channel: Spring Community Forums - Data
Viewing all articles
Browse latest Browse all 297

jpa2 and lazy loading

$
0
0
hi

i save an Advertisement object,
after i save message

i try to access message from Advertisement but it's null

Code:

@Entity
public class Advertisement implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @OneToMany(mappedBy="id", cascade={CascadeType.REMOVE}, fetch=FetchType.LAZY)
    private Set<Message> messages;
}

@Entity
public class Message implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @ManyToOne
    private Advertisement advertisement;
}

test unit
Code:

        Advertisement ads = new Advertisement();
        ads = advertisementRepo.save(ads);

        assertNotNull(ads);
        assertNotNull(ads.getId());

        Message message = new Message();
        message.setAdvertisement(ads);
        message = msgRepo.save(message);
 
        ads = advertisementRepo.findOne(ads.getId());
        ads.getMessages(); //return null

why ads.getMessages() is not return messages?

thanks

Viewing all articles
Browse latest Browse all 297

Trending Articles