Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8833163
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:42:50+00:00 2026-06-14T08:42:50+00:00

I have such code: Session session = HibernateSessionFactory.sessionFactory.openSession(); System.out.println(—————— + session.get(User.class, (long) 10)); System.out.println(——————

  • 0

I have such code:

Session session = HibernateSessionFactory.sessionFactory.openSession();

    System.out.println("------------------" + session.get(User.class, (long) 10));
    System.out.println("------------------" + session.createSQLQuery("SELECT  * FROM  diploma.tbl_users Where id = 10").addEntity(User.class).uniqueResult());

First row return null.
The second return valid record.

But if I change places:

System.out.println("------------------" + session.createSQLQuery("SELECT  * FROM  diploma.tbl_users Where id = 10").addEntity(User.class).uniqueResult());
    System.out.println("------------------" + session.get(User.class, (long) 10));

Both rows return correct result:

This is my hibernate session factory:

public class HibernateSessionFactory {

public static SessionFactory sessionFactory = new Configuration().configure("/META-INF/hibernate.cfg.xml")
        .buildSessionFactory();

 }

Why session.get(User.class, (long) 10)) return null ?

UPDATE
hibernate config:

<hibernate-configuration>
<session-factory>

    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/diploma</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password">password</property>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
    <property name="hibernate.connection.charSet">UTF-8</property>
    <property name="hibernate.hbm2ddl.auto">update</property>

    <property name="show_sql">true</property>
    <property name="format_sql">true</property>

    .........................................
    <mapping class="edu.test.entities.User" />
            ..................................

</session-factory>

User.java

@Entity
@Table(name = "tbl_Users")
public class User extends BaseEntity {

@NotEmpty
@Column(name = "Name")
private String name;

@NotEmpty
@Column(name = "Surname")
private String surname;

@NotEmpty
@Column(name = "Login")
private String login;

@NotEmpty
@Size(min=6, max=20)
@Column(name = "Password")
private String password;

@NotEmpty
@Column(name = "Email")
private String email;

@NotEmpty
@Column(name = "Phone")
private String phone;

@ManyToOne
@JoinColumn(name = "Role", nullable = false)
private Roles role;

    // getters and setters

Id field from base entity

   @MappedSuperclass
   public class BaseEntity implements Serializable {    
    @Id
@Column(name = "id", unique = true, nullable = false)
@GeneratedValue
private Long id;

UPDATE 2
The problem was in mapping file @JoinColumn(name = "Role", nullable = false)
private Roles role;
I have specified that Role cannot be null and the record that I have tried to retrieve with id 10 have null Role foreign key. So I change nullable = true and it works.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T08:42:53+00:00Added an answer on June 14, 2026 at 8:42 am

    Hibernate implements Identity Map PoEAA pattern, where the Hibernate session plays the role of the map. When you call .addEntity(), the loaded entities become associated with the Hibernate session.

    Then when you call Hibernate session’s get method it first checks the entity cache and returns a existing entity if present.

    So in the first statement when you call get the entity is not yet present in the entity map. In the second snippet the entity is being cached with .addEntity() method.

    update So the problem is that reference to the role is declared with nullable = false, and there’s no such a role in the database.

    See also: http://martinfowler.com/eaaCatalog/identityMap.html

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have such code (copy paste from wiki). Its multiplication of those big numbers
I have such code: $updateSQL = UPDATE `Rooms` R SET R.Revenue = {$s['Revenue']}, R.Ave_rent
I have such code: <h:inputText id=input value=#{bean.input}> <f:convertNumber /> <rich:ajaxValidator event=onblur /> </h:inputText> I
I have such code in my JSF template: <h:form> <table id=users cellspacing=0> <a4j:repeat var=person
i have such code var prj = _dataContext.Project.FirstOrDefault(p => p.isPopular == true); if (prj
I have such code: function allValid() { $('input').each(function(index) { if(something) { return false; }
I have such code: Prelude> let n = [1,2,3,4] Prelude> n [1,2,3,4] Prelude> 0:n
I'm trying to understand how php function usort works. I have such code: <?php
I have a such code for Lithuanian declension: <?php $word = namas; $string =
Let's say I have such PoC code snippet for working with canvas raw pixel

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.