2

I am using spring 4.1.4.RELEASE + hibernate 4.3.6.Final, here is my entity code:

public class BaseEntity implements Serializable {
}


public class MarketInfo extends BaseEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private int id;

    @Column(name = "market_id", unique = true, length = 15)
    private String marketId;

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "market")
    private List<MarketChannelGroup> channelGroups;

    public List<MarketChannelGroup> getChannelGroups() {
        return channelGroups;
    }

    public void setChannelGroups(List<MarketChannelGroup> channelGroups) {
        this.channelGroups = channelGroups;
    }

...
}

public class MarketChannelGroup extends BaseEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private int id;


    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "market_id", referencedColumnName = "market_id")
    private MarketInfo market;
...
}

From my test I can see the channelGroups in MarketInfo is working fine (if I don't call getChannelGroups(), then channelGroups is null), however if I call getChannelGroups(), the MarketInfo inside each MarketChannelGroup gets fetched, while this should not happen since market's fetch mode is FetchType.LAZY.

From console I do see the following hibernate log when I call its getter:

Hibernate: select channelgro0_.market_id as market_i5_12_1_, channelgro0_.id as id1_9_1_, channelgro0_.id as id1_9_0_, channelgro0_.channel_group_id as channel_2_9_0_, channelgro0_.channel_group_name as channel_3_9_0_, channelgro0_.channel_group_type as channel_4_9_0_, channelgro0_.market_id as market_i5_9_0_ from market_channel_group channelgro0_ where channelgro0_.market_id=?
Hibernate: select marketinfo0_.id as id1_12_0_, marketinfo0_.enable_flag as enable_f2_12_0_, marketinfo0_.enable_time as enable_t3_12_0_, marketinfo0_.market_id as market_i4_12_0_, marketinfo0_.market_name as market_n5_12_0_, marketinfo0_.stb_count as stb_coun6_12_0_ from market_info marketinfo0_ where marketinfo0_.market_id=?
Hibernate: select marketinfo0_.id as id1_12_0_, marketinfo0_.enable_flag as enable_f2_12_0_, marketinfo0_.enable_time as enable_t3_12_0_, marketinfo0_.market_id as market_i4_12_0_, marketinfo0_.market_name as market_n5_12_0_, marketinfo0_.stb_count as stb_coun6_12_0_ from market_info marketinfo0_ where marketinfo0_.market_id=?
Hibernate: select marketinfo0_.id as id1_12_0_, marketinfo0_.enable_flag as enable_f2_12_0_, marketinfo0_.enable_time as enable_t3_12_0_, marketinfo0_.market_id as market_i4_12_0_, marketinfo0_.market_name as market_n5_12_0_, marketinfo0_.stb_count as stb_coun6_12_0_ from market_info marketinfo0_ where marketinfo0_.market_id=?

could anyone help?

UPDATE

there is no optional method for ManyToOne annotation, so the solution in OneToOne doesn't work for my case.

seaguest
  • 2,222
  • 5
  • 21
  • 38
  • 1
    Try with `optional=false` in your ManyToOne association. Refer - http://stackoverflow.com/a/17987718/1282369 – tsatiz Sep 28 '16 at 13:16
  • Possible duplicate of [Hibernate: one-to-one lazy loading, optional = false](http://stackoverflow.com/questions/17987638/hibernate-one-to-one-lazy-loading-optional-false) – tsatiz Sep 28 '16 at 13:18
  • @tsatiz, optional=false is not recognized by hibernate here, only OneToOne has that optional method. – seaguest Sep 29 '16 at 02:04
  • ok, can you try to create a field for market_id column and refer the logical name in the relation. something like `private int marketId; @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "marketId", referencedColumnName = "market_id", insertable = false, updatable = false) private MarketInfo market; ` – tsatiz Sep 29 '16 at 02:31
  • about the optional=false option, hibernate 5 has it for `@ManyToOne`. I didn't check which version you are using. Sorry about that. – tsatiz Sep 29 '16 at 02:40
  • did u solve it? – Ankit Bansal Apr 21 '17 at 06:31
  • @AnkitBansal, no, I didn't, I finally found hibernate is not so flexible, and I decided not to use it anymore. – seaguest Apr 22 '17 at 09:04
  • 1 moment, "lazy" means: it (the list of MarketChannelGroups) is not loaded, when you load one `MarketInfo` ...but of course it *should* load when you call `getChannelGroups()` (in a transactional context) ...that's how lazy works....what would you expect to return(, when you call `getChannelGroups()`...and no db select takes place..)? – xerx593 Feb 12 '18 at 04:26

3 Answers3

0

Move the @OneToMany annotation from the declaration to the getter method, like this:

private List<MarketChannelGroup> channelGroups;

@OneToMany(fetch = FetchType.LAZY, mappedBy = "market")
public List<MarketChannelGroup> getChannelGroups() {
    return channelGroups;
}
Maxinator
  • 61
  • 4
0

How is your configuration archive? Look if you are using the filter OpenEntityManagerInViewFilter. If you are using it, always that you call the method Lazy getChannelGroups() the hibernate will get fetch inside each element.

0

You have to take oneToMany annotation to getter if @id is on the getter. Lazy works but if you or some framework get the lazy property in that transction the get method fire the select.