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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T15:27:43+00:00 2026-05-19T15:27:43+00:00

I have a table where the the parent object has an optional many-to-one relationship.

  • 0

I have a table where the the parent object has an optional many-to-one relationship. The problem is that the table is setup to default the fkey column to 0.

When selecting, using fetch=”join”, etc– the default of 0 on the fkey is being used to try over and over to select from another table for the ID 0. Of course this doesn’t exist, but how can I tell Hibernate to treat a value of 0 to be the same as NULL– to not cycle through 20+ times in fetching a relationship which doesn’t exist?

<many-to-one name="device" lazy="false" class="Device" not-null="true" access="field" cascade="none" not-found="ignore">
<column name="DEVICEID" default="0" not-null="false"/>

  • 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-19T15:27:44+00:00Added an answer on May 19, 2026 at 3:27 pm

    I was able to fix this by creating an id-long type which extends the built in Long type, but if the id returned from SQL was 0, return null instead. This kept the allowance of default 0s in our DB while getting hibernate to stop doing lazy fetches.

    public class IdentifierLongType extends LongType implements IdentifierType {
    
    @Override
    public Object get(ResultSet rs, String name) throws SQLException {
        long i = rs.getLong(name);
        if (i == 0) {
            return null;
        } else {
            return Long.valueOf(i);
        }
    }
    

    }

    The reason for enforcing explicit default 0 is that Oracle handles indexing and null values oddly, suggesting better query performance with explicit values vs. ‘where col is [not] null’

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

Sidebar

Related Questions

I have db table with parent child relationship as: NodeId NodeName ParentId ------------------------------ 1
I have a table which defines a child-parent relationship between nodes: CREATE TABLE node
I have the following table relationship in my database: Parent / \ Child1 Child2
Lets say I have two tables - child and parent with many-to-one relation. What
I have a parent object, Post, which has the following children. has_one :link has_one
I have a table with 6400 rows. This is a parent-children table, it has
I have table inside a div tab. The table has 40 rows in it
Suppose I have a table nodes where I store a tree. Each node has
I am Using LINQ to query a table that has a foreign key for
When you have parent-child tables and you wish to use a DetailsView to edit

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.