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 1032487
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T14:05:03+00:00 2026-05-16T14:05:03+00:00

Considering the following model: USER Long: PK String: firstName String: lastName USER_EXT Long: PK

  • 0

Considering the following “model”:

USER
    Long: PK
    String: firstName
    String: lastName

USER_EXT
    Long: PK
    String: moreInfo
    Date: lastModified

I’m trying to find/create the correct Hibernate mapping (using Annotations) such that, with an HQL query as simple as “from User”, it would generate the following SQL:

select firstName, moreInfo from USER, USER_EXT where user.pk = user_ext.pk

I’ve tried everything, from using @Secondarytable to @OneToOne association, but I can’t make it work.

The best result I have now is with the @OneToOne association which generate multiple SQL queries, one to fetch rows in USER and for each rows in the resultset a select query from USER_EXT.

This is quite ineffective.

Any idea ?

  • 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-05-16T14:05:03+00:00Added an answer on May 16, 2026 at 2:05 pm

    Choosing between OneToOne and Secondarytable somehow depends on the object model (i.e. if you want an entity for the user extension). I chose to use a OneToOne association and two entities.

    For the User (note the use of the PrimaryKeyJoinColumn for the shared primary key):

    @Entity
    public class User {
    
        @Id private Long id;
        private String firstName;
        private String lastName;
    
        @OneToOne
        @PrimaryKeyJoinColumn
        private UserExt userExt;
    
        public UserExt getUserExt() {
            return userExt;
        }
        public void setUserExt(UserExt userExt) {
            this.userExt = userExt;
        }
        public Long getId() {
            return id;
        }
        public void setId(Long id) {
            this.id = id;
        }
        public String getFirstName() {
            return firstName;
        }
        public void setFirstName(String firstName) {
            this.firstName = firstName;
        }
        public String getLastName() {
            return lastName;
        }
        public void setLastName(String lastName) {
            this.lastName = lastName;
        }
    }
    

    And the UserExt:

    @Entity
    public class UserExt {
    
        @Id private Long id;
        private String moreInfo;
        @Temporal(TemporalType.DATE)
        private Date lastModified;
        public Long getId() {
            return id;
        }
        public void setId(Long id) {
            this.id = id;
        }
        public String getMoreInfo() {
            return moreInfo;
        }
        public void setMoreInfo(String moreInfo) {
            this.moreInfo = moreInfo;
        }
        public Date getLastModified() {
            return lastModified;
        }
        public void setLastModified(Date lastModified) {
            this.lastModified = lastModified;
        }   
    }
    

    With the above entities, the following HQL query:

    select u.firstName, u.userExt.moreInfo from User u
    

    Generates the following SQL query:

    select
      userx0_.firstName as col_0_0_,
      userextx1_.moreInfo as col_1_0_ 
     from
      User userx0_,
      UserExt userextx1_ 
     where
      userx0_.id=userextx1_.id
    

    Which is the expected result.

    PS: JPA 1.0 actually provides poor support of derived identifiers (things are much better in JPA 2.0) and you will have to set the Id manually on the UserExt, unless you use an Hibernate specific foreign generator. See the question below for details.

    Related Question

    • JPA Hibernate One-to-One relationship
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Considering following code public class A { public static void main(String[] args) { new
Considering the following database : Post -> User The entities : class User {
Considering the following view excerpt : <%= Html.TextBox(Something, Model.Property.SubProperty.Value) %> I can handle the
Considering the following Scala snippet: case class Foo(v1: String, v2: Int, v3: Any) def
Considering the following TSQL: INSERT INTO Address(Street1, City, State, ZipCode) SELECT Street1, City, StateCode,
Considering the following table: someId INTEGER #PK ageStart TINYINT(3) ageEnd TINYINT(3) dateBegin INTEGER dateEnd
considering the following enum: public enum LeadStatus { Cold = 1, Warm = 2,
quick question on GWT from a newbie. Considering the following code: FlowPanel block =
Considering this article Developing a MVC component for Joomla , following is the code
I have the following doubt. I am considering the option to have html tags

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.