grantk
grantk

Reputation: 4058

JPA Entity Creation - Objects vs. "Primitives"

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?

Upvotes: 3

Views: 1997

Answers (2)

Affe
Affe

Reputation: 47984

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!

Upvotes: 2

Travis
Travis

Reputation: 3

It seems like your asking whether or not to use the Integer Object or the int data type. If this is so, I would recommend the int data type. Java came up with them when they realized that putting number values into the Integer and Double objects was both a hassle and a waste of memory space. An object refers to a location which stores all the values that you have put into it, thus allowing you to put in multiple variables. A data type simply stores the data.

Upvotes: 0

Related Questions