0

Hibernate newbie here.

So I have a User object with many Opportunity objects:

@Entity
public class User implements Serializable{

    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long userId;
    private String firstName;
    private String lastName;
    private String email;
    @OneToMany(fetch=FetchType.EAGER, cascade=CascadeType.ALL, mappedBy="user")
    private List<Opportunity> opportunities = new ArrayList();
    @Temporal(TemporalType.TIMESTAMP)
    private Date creationDate;

And I have an Opportunity object with a ManyToOne relation with a User:

@Entity
public class Opportunity implements Serializable{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long opportunityId;
    @ManyToOne(fetch=FetchType.EAGER, cascade=CascadeType.ALL)
    @JoinColumn(name="userId", nullable=false)
    private User user;
    private String listingUrl;
    private String position;
    private String organization;
    private String location;
    @Column(columnDefinition="text")
    private String notes;
    @Enumerated(EnumType.STRING)
    private Action action;
    @Temporal(TemporalType.TIMESTAMP)
    private Date creationDate;
    @Temporal(TemporalType.DATE)
    private Date dueDate;

All get/set methods are emitted here just to save space. What I assumed would happen when I used the getOpportunities() method of the User class, was that it would return all the Opportunity objects in the database. I figured that the annotations would pull them automatically for me. However, it did not. So my question is, do the annotations help retrieve data from the database? How? Or are they just used for creating the database? If I want to get all the opportunities, do I have to write a custom transaction/query?

  • Annotations don't "do" anything, other than signal to the JPA provider that there is a relation at that field. The JPA provider is the thing that will pull back a field when it is needed. Since you don't post the code where you called "getOpportunities()" people are left to guess whether the EntityManager/Transaction are still open. The JPA provider has a log which tells you when a field is pulled back from the database; I'd suggest that you look at it. – Neil Stockton Jun 11 '16 at 18:02
  • This should have loaded your opportunities. I don't see table name associated for your Entities `@Table(). Are those just skipped here or they are just not there. – Jimmy Jun 11 '16 at 18:18
  • @NeilStockton I figured it out and got the opportunities! Your answer pushed me in the right direction. Thanks! Now I'm wondering about something else. It seems that when I first retrieved my User object from the database, that is when all the Opportunity objects were fetched and put in the List. After that, they are never updated again even if I put another Opportunity in the database and call getOpportunities(). So if I add another Opportunity to the database, what is the best way to access it? Should I also add it to the List? That way it's available in the session and it's persisted. –  Jun 11 '16 at 19:17
  • Typically, if thread A is adding a new opportunity to the database and thread A wants to see that change then thread A would add that to the opportunities list. However, if thread B adds a new opportunity to the database and thread A is already in progress then thread A will probably have to close its transaction/session and reopen a new one (re-fetching everything from the database). For more details read up on isolation levels. – Pace Jun 11 '16 at 19:31
  • @Jimmy Why exactly are you requiring to see a `@Table` annotation? That is, firstly, optional (JPA defines default table names for entities), secondly, could be in XML, and thirdly, has no bearing on "the problem" – Neil Stockton Jun 12 '16 at 05:48
  • With hibernate impl, I doubt if you can mix annotation and xml on same class. He need to be aware that default name generated is what he is looking for. Regarding his actual problem, based on the mapping , i would not even worry about transactions as he is loading it eagerly directly in hibernate session, and assuming 3+ implementation, would not even worry about proxy return. My doubt is in some empty exception handler . If I were him , I would enable all log to trace level ? that will log all the translated sql queries fired, parameters passed and returned result which can be helpful. – Jimmy Jun 12 '16 at 16:54

0 Answers0