Skip to content

Help: Hibernate Composite ID

I’m calling for help here guys! I have a database with a bunch of tables that are devoid of primary keys. When I auto-generated the xml and Java files with Hibernate Tools, I got an Id class in addition to the bean class for each mapping. But, all of the properties are stored in the Id class and the bean class simply has one property for the Id class. It looks something like this:

package com.boohbah.data.bean;

public class Notes implements java.io.Serializable {

	private NotesId id;

	public Notes() {
	}

	public Notes(NotesId id) {
		this.id = id;
	}

	public NotesId getId() {
		return this.id;
	}

	public void setId(NotesId id) {
		this.id = id;
	}

}

And the NotesId class, which has all the mapped properties.

package com.boohbah.data.bean;

public class NotesId implements java.io.Serializable {

	private String conum;

	private Integer coline;

	private int notekey;

	private int seq;

	private String txt;

	private Boolean newparagraph;

	public NotesId() {
	}

	public NotesId(int notekey, int seq) {
		this.notekey = notekey;
		this.seq = seq;
	}

	public NotesId(String conum, Integer coline, int notekey, int seq,
			String txt, Boolean newparagraph) {
		this.conum = conum;
		this.coline = coline;
		this.notekey = notekey;
		this.seq = seq;
		this.txt = txt;
		this.newparagraph = newparagraph;
	}

	public String getConum() {
		return this.conum;
	}

	public void setConum(String conum) {
		this.conum = conum;
	}

	public Integer getColine() {
		return this.coline;
	}

	public void setColine(Integer coline) {
		this.coline = coline;
	}

	public int getNotekey() {
		return this.notekey;
	}

	public void setNotekey(int notekey) {
		this.notekey = notekey;
	}

	public int getSeq() {
		return this.seq;
	}

	public void setSeq(int seq) {
		this.seq = seq;
	}

	public String getTxt() {
		return this.txt;
	}

	public void setTxt(String txt) {
		this.txt = txt;
	}

	public Boolean getNewparagraph() {
		return this.newparagraph;
	}

	public void setNewparagraph(Boolean newparagraph) {
		this.newparagraph = newparagraph;
	}

	public boolean equals(Object other) {
		if ((this == other))
			return true;
		if ((other == null))
			return false;
		if (!(other instanceof NotesId))
			return false;
		NotesId castOther = (NotesId) other;

		return ((this.getConum() == castOther.getConum()) || (this.getConum() != null
				&& castOther.getConum() != null && this.getConum().equals(
				castOther.getConum())))
				&& ((this.getColine() == castOther.getColine()) || (this
						.getColine() != null
						&& castOther.getColine() != null && this.getColine()
						.equals(castOther.getColine())))
				&& (this.getNotekey() == castOther.getNotekey())
				&& (this.getSeq() == castOther.getSeq())
				&& ((this.getTxt() == castOther.getTxt()) || (this.getTxt() != null
						&& castOther.getTxt() != null && this.getTxt().equals(
						castOther.getTxt())))
				&& ((this.getNewparagraph() == castOther.getNewparagraph()) || (this
						.getNewparagraph() != null
						&& castOther.getNewparagraph() != null && this
						.getNewparagraph().equals(castOther.getNewparagraph())));
	}

	public int hashCode() {
		int result = 17;

		result = 37 * result
				+ (getConum() == null ? 0 : this.getConum().hashCode());
		result = 37 * result
				+ (getColine() == null ? 0 : this.getColine().hashCode());
		result = 37 * result + this.getNotekey();
		result = 37 * result + this.getSeq();
		result = 37 * result
				+ (getTxt() == null ? 0 : this.getTxt().hashCode());
		result = 37
				* result
				+ (getNewparagraph() == null ? 0 : this.getNewparagraph()
						.hashCode());
		return result;
	}

}

The problem is that I only have mappings for the PO’s. And when I run even the most simple hql query, I get a bunch of null objects returned; each one corresponding to a row in the database. I can’t query NotesId, because it doesn’t have a mapping file. Here is the mapping file for the Notes class.

When I do a simple query like “from Notes”, I will just get a list that has the same number of elements as there are rows for the Notes table in the database, but they are all null!

Please help if you can!

PISS!

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

3 Comments

  1. eceppda wrote:

    My suggestion is to stop using generated code.

    Friday, March 2, 2007 at 1:27 pm | Permalink
  2. eokuwwy wrote:

    I said screw it and gave up. I kept most of the generated code though. I just got rid of that composite-id BS and the Id classes. Works great now!

    Friday, March 2, 2007 at 3:53 pm | Permalink
  3. SteveT wrote:

    The question may be stale now, but the problem seems to be that the attributes are buried in the NotesId class and are not directly accessible through the Notes class.

    You could try adding accessors to the Notes class like this:

    public String getConum() {
    return this.id.getConum();
    }

    Friday, March 7, 2008 at 1:39 pm | Permalink

2 Trackbacks/Pingbacks

  1. [...] time ago, I posted a question about Composite-IDs in Hibernate. Not surprisingly, I didn’t get much for a response. Usually, each table in the database has [...]

  2. [...] time ago, I posted a question about Composite-IDs in Hibernate. Not surprisingly, I didn’t get much for a response. Usually, each table in the database has [...]

Post a Comment

Your email is never published nor shared. Required fields are marked *
*
*