Hibernate and empty field

B

Biagio

Hi everyone,

I have a Customer table that has the field in relation CodBanca @
OneToOne to Bank table.
Not always the field is enhanced, because a client may not have a
bank.

If I leave the field blank in a row, the query gives me error.
Can be mapped a relationship with an optional field or do I have to
find another solution?
Using a JTable is useful to have the relation mapped with the
annotation.

My code is

@ OneToOne ()
@ JoinColumn (name = "codbanca", unique = true, nullable = true,
Insertable = false, updatable = false)
private Banks banks;

Suggestions?

Biagio
 
A

Arved Sandstrom

Hi everyone,

I have a Customer table that has the field in relation CodBanca @
OneToOne to Bank table.
Not always the field is enhanced, because a client may not have a
bank.

If I leave the field blank in a row, the query gives me error.
Can be mapped a relationship with an optional field or do I have to
find another solution?
Using a JTable is useful to have the relation mapped with the
annotation.

My code is

@ OneToOne ()
@ JoinColumn (name = "codbanca", unique = true, nullable = true,
Insertable = false, updatable = false)
private Banks banks;

Suggestions?

Biagio

I assume that "private Banks banks" was suggestive of the expected
relationship? For tables named Customer and Bank, and a real-world
relation (one bank, many customers), I'd expect (for field, not
property, mapping)

Customer.java:

@ManyToOne
@JoinColumn(name="codbanca")
private Bank bank;

Bank.java:

@OneToMany(mappedBy="bank")
private Customer customers;

1. The above is set up as a bi-directional relationship.
2. On OneToOne, OneToMany, ManyToOne etc, leave out "optional" if it's
true, which is the default. And you do in fact want the default here.
3. We expect a column named "codbanca" in the Customer table.
4. OneToMany has default fetch type LAZY, M:1 is default fetch type EAGER.

Since the relationship _is_ optional, if you're querying on Customer you
may not get a Bank retrieved as well. Be advised that in the _absence
of_ joins (however you code them) that a query retrieving Customer will
retrieve related Bank objects in separate SELECTs; this can be
inefficient. Having said that, on a straightforward query against
Customer you ought not have any problems if some "codbanca" values are null.

HTH,
AHS
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top