-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Field 'a_id' doesn't have a default value
PostPosted: Mon Oct 22, 2012 3:56 pm 
Newbie

Joined: Mon Oct 22, 2012 3:24 pm
Posts: 2
Hi all:

I've seen people encountering similar error messages, but the cause of mine doesn't seem to be similar to any of theirs. Any pointers on how to solve this or an explanation on what's going on is greatly appreciated! Here are the tables from my legacy DB:

Code:
CREATE TABLE table_a (
   a_id int(10) unsigned NOT NULL AUTO_INCREMENT,
   x tinyint(3) unsigned NOT NULL,
   PRIMARY KEY (a_id,x),
)

CREATE TABLE table_b (
   b_id int(10) unsigned NOT NULL AUTO_INCREMENT,
   x tinyint(3) unsigned NOT NULL,
   a_id int(10) unsigned DEFAULT NULL,
   PRIMARY KEY (b_id,x),
   KEY fk_table_b_table_a1 (a_id,x),
   CONSTRAINT fk_table_b_table_a1 FOREIGN KEY (a_id, x) REFERENCES table_a (a_id, x),
)


And the following were generated using Hibernate Reverse Engineering, truncated for brevity:

Code:
@Embeddable
public class TableAId {
    private int a_id;
    private byte x;
}

@Entity
@Table(name = "table_a")
public class TableA {
   @AttributeOverrides({
      @AttributeOverride(name = "a_id", column = @Column(name = "a_id", nullable = false)),
             @AttributeOverride(name = "x", column = @Column(name = "x", nullable = false))
   })
    @EmbeddedId
    private TableAId id;
}

@Embeddable
public class TableBId {
    private int b_id;
    private byte x;
}

@Entity
@Table(name = "table_b")
public class TableB {

    @AttributeOverrides({
        @AttributeOverride(name = "b_id", column = @Column(name = "b_id", nullable = false)),
        @AttributeOverride(name = "x", column = @Column(name = "x", nullable = false))
    })
    @EmbeddedId
    private TableBId id;

    @JoinColumns({
        @JoinColumn(name = "a_id", referencedColumnName = "a_id", nullable = false, insertable = false, updatable = false),
        @JoinColumn(name = "x", referencedColumnName = "x", nullable = false, insertable = false, updatable = false)
    })
    @ManyToOne(fetch = FetchType.LAZY)
    private TableA TableA;
}


Whenever I do an insert of TableB, I'm getting an error that says, "Field 'a_id' doesn't have a default value."

Removing _all_ "nullable=false" (why all? If not, I'd get a different error: "Mixing nullable and non nullable columns...") seems to "fix" it. But this is not optimal, because they _are_ non nullable. Many thanks in advance!


Top
 Profile  
 
 Post subject: Re: Field 'a_id' doesn't have a default value
PostPosted: Mon Oct 22, 2012 9:45 pm 
Newbie

Joined: Mon Oct 22, 2012 9:28 pm
Posts: 1
Hi,
Dont know about reverse engineering but I encountered a similar error. Change your generation strategy to @GeneratedValue(strategy=GenerationType.AUTO) or @GeneratedValue(strategy=GenerationType.SEQUENCE). I have a few theories as to why it works this way, but I leave them to fuether investigation.


Top
 Profile  
 
 Post subject: Re: Field 'a_id' doesn't have a default value
PostPosted: Thu Oct 25, 2012 2:35 pm 
Newbie

Joined: Mon Oct 22, 2012 3:24 pm
Posts: 2
mr_frank, unfortunately that didn't solve the problem. By the way, I couldn't seem to get @GeneratedValue to work; I remember reading somewhere that says @GeneratedValue doesn't work with @Embeddable. I'm not sure if that's correct, but from a brief testing, my finding seems to be consistent. But I think that's a topic for a different thread.

I think source of the problem is the complication of having (part of the) the composite primary key of the child table as a foreign key to the parent table. For some reasons, with this setup, when setTableA() is called, a_id doesn't get set, and so it defaults to null. The "nullable = false" element causes the error "Field 'a_id' doesn't have a default value." Therefore, removing this element--nullable--is not the proper solution. The closest thing that I could find out there about this sort of problem is detailed here http://beavercreekconsulting.com/blog/2008/10/hibernate-annotations-for-a-one-to-many-mapping/. It's a bit outdated, however, and it doesn't address multi-level of similar setup.

However, I found working solution. I added the following field in TableB entity, its getter/setter not shown for brevity:

Code:
@Column(name = "a_id")
private int aId;


And then, in setTableA()

Code:
public void setTableA(TableA tableA) {
        this.tableA = tableA;
        this.aId = tableA.getId().getAId();
}


I appreciate any comments, thoughts, or a better solution!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.