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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T15:03:54+00:00 2026-05-21T15:03:54+00:00

I have a question for entity creation. Say I have the following tables create

  • 0

I have a question for entity creation. Say I have the following tables

create table "foo" (
  "id"          integer primary key,
  "name"        varchar
);

create table "bar" (
  "id"          integer primary key,
  "name"        varchar,
  "fk_foo"      integer references foo(id)
);

I wonder which would be best for entities, to use a foo object for the foreign key reference in the entity class or to use an integer in the entity.

Using Netbeans 6.9 I generated the following entities:

@Entity
@Table(name = "bar")
@NamedQueries({
    @NamedQuery(name = "Bar.findAll", query = "SELECT b FROM Bar b"),
    @NamedQuery(name = "Bar.findById", query = "SELECT b FROM Bar b WHERE b.id = :id"),
    @NamedQuery(name = "Bar.findByName", query = "SELECT b FROM Bar b WHERE b.name = :name")})
public class Bar implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @Column(name = "id")
    private Integer id;
    @Column(name = "name")
    private String name;
    @JoinColumn(name = "fk_foo", referencedColumnName = "id")
    @ManyToOne
    private Foo foo;

    public Bar() {
    }


@Entity
@Table(name = "foo")
@NamedQueries({
    @NamedQuery(name = "Foo.findAll", query = "SELECT f FROM Foo f"),
    @NamedQuery(name = "Foo.findById", query = "SELECT f FROM Foo f WHERE f.id = :id"),
    @NamedQuery(name = "Foo.findByName", query = "SELECT f FROM Foo f WHERE f.name = :name")})
public class Foo implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @Column(name = "id")
    private Integer id;
    @Column(name = "name")
    private String name;
    @OneToMany(mappedBy = "foo")
    private Collection<Bar> barCollection;

    public Foo() {
    }

Now when I have to go and create a new Bar object I have to also create a new Foo object. So I create the following bar. Keep in mind I left setters and all but default constructors out of the entities.

Bar bar1 = new Bar(1);
bar1.setName(“wheatly”);
bar1.setFoo(new Foo(1));

As opposed to using a field type of integer for Foo:

Bar bar2 = new Bar(1);
bar2.setName(“GlaDOS”);
bar2.setFoo(1);

I would prefer the second method myself. Is there a reason that it is better to go with the first route? One problem I have run into is on unmarshalling XML I have combined entities and JAXB objects and in their case the Foo object gets set to a completely null Foo object instead of a Foo Object with id of 1.

What do you think?

  • 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-21T15:03:55+00:00Added an answer on May 21, 2026 at 3:03 pm

    Well, it is called Object Relational Mapping :). If you’re not finding value in JPA’s additional features over simpler JDBC wrappers, there are much more light-weight alternatives that can stuff database column values into an object.

    The first thing mapping your objects does for you is allows you to write joins naturally in JPQL. You can’t make arbitrary joins in JPQL, they have to be defined as object mappings.

    e.g., if all you have on bar is the number value of the id, and you want to find one with a certain foo you end up with

    select b from Bar b, Foo f where bar.fooId = f.id and f.name = :fooName;
    

    whereas if you mapped it the normal object-reference way you can write

    select b from Bar b where b.foo.name = :fooName;
    

    Depending on what database you’re using and if/how well it rewrites queries, the cartesian product in that first query can be bad news.

    Secondly, if you don’t map your objects, you can’t use transitive persistence. You’re back to explicitly causing each separate insert and update using the EntityManager, as if it’s just a JDBC wrapper. Abstracting away all the explicit persist/merge insert/update into the ORM layer via transitive persistence (CASCADE settings) is one of the major things JPA offers over simpler JDBC wrappers.

    e.g., if you mark your List of Bar with CascadeType.PERSIST in the @OneToMany, then you can make new bars by doing this:

    beginTransaction();
    Foo foo = em.find(Foo.class, 1);
    Bar newBar = new Bar();
    //set up your Bar
    foo.getBarCollection().add(newBar);
    commitTransaction();
    

    done!

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

Sidebar

Related Questions

I have a question regarding foreign key in entity framework(EF4). Lets say i have
I have the following in a Question entity: @NamedQuery(name = Question.allApproved, query = SELECT
I have a question about how Hibernate persists entity relations. Let's say I have
I have a question regarding DataBase design. I have a Entity Table named: CARS
I have a simple question regarding Entity declaration in JPA. I have an entity
Following on from the excellent answer to my previous question: Linq Entity Framework generic
I have a question about entity creations that is specific to a student information
I have a question. I have used ADO and Entity Framework. No doubt, Entity
i have one question, i have this class in javascript: //FactVal UTIL.Classes.FactVal = function(entity,
i have a simple question about entity . i have a simple method it

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.