Hello, I am having problems mapping a property with a class type of Master class from a class called BattingPost. There is one Master record for each Battingpost.playerId . The issue is that the Master.PlayerID field is not a primary key in the Master table.
I want to load the BattingPost class and then load the associated Master record when I view an individual BattingPostRecord.
I followed what the Hibernate documentation said about using property-ref but it is not even close working as intended.
I am encountering two problems. Problem 1)
The following code is loading every instance of the Master class for each BattingPost class . I get a parsing error when I include lazy="true" in the mapping file
SessionFactory factory = HibernateUtil.getSessionFactory(); Session session = factory.openSession(); Transaction tx = session.beginTransaction(); retVals.addAll(session.createQuery("from baseball.hibernate.Battingpost").list()); tx.commit(); session.close();
Problem two )
I keep getting a "Null property for Master" message .When trying to instantiate my BattingPost class . This makes no sense as apparently hibernate had already loaded all Master class associations in Problem .
There is apparently no useful documentation on retrieving a property that is lazy loaded and you don't have the Primary key.
This is leading me to thinking that Hibernate is completely useless when working with non primary key associations. Below is what I am trying to do.
public static List getPlayer(String playerid) throws Exception { List retVals = new ArrayList();
SessionFactory factory = HibernateUtil.getSessionFactory(); Session session = factory.openSession(); Transaction tx = session.beginTransaction();
retVals.addAll(session.createQuery("from baseball.hibernate.Battingpost b where b.playerId='" + playerid + "'").list());
ArrayList master_vos = new ArrayList(); ArrayList battingpost_vos = new ArrayList(); for (Iterator it = retVals.iterator(); it.hasNext(); ) { baseball.web.struts.BattingpostForm battingpostFormTemp = new baseball.web.struts.BattingpostForm(); baseball.hibernate.Battingpost battingpostPersistence = (baseball.hibernate.Battingpost)it.next();
This Causes the error-> Master m = ( Master)session.load(Master.class, battingpostPersistence.getMaster());
battingpostPersistence.setMaster(m); TranslatorUtil.copyPersistenceToVo(battingpostPersistence, battingpostFormTemp); battingpost_vos.add(battingpostFormTemp); } retVals = battingpost_vos; tx.commit(); session.close(); return retVals; }
These are the two mapping files and Java classes
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Generated Jul 18, 2009 3:03:12 PM by Hibernate Tools 3.2.2.GA --> <hibernate-mapping> <class name="baseball.hibernate.Battingpost" table="battingpost" catalog="baseball"> <composite-id name="id" class="baseball.hibernate.BattingpostId"> <key-property name="yearId" type="short"> <column name="yearID" /> </key-property> <key-property name="round" type="string"> <column name="round" length="10" /> </key-property> <key-property name="playerId" type="string"> <column name="playerID" length="10" /> </key-property> </composite-id> <property name="yearId" type="short" insert="false" update="false"> <column name="yearID" /> </property> <property name="round" type="string" insert="false" update="false"> <column name="round" length="10" /> </property> <property name="playerId" type="string" insert="false" update="false"> <column name="playerID" length="10" /> </property> <property name="teamId" type="string"> <column name="teamID" length="3" not-null="true" /> </property> <property name="lgId" type="string"> <column name="lgID" length="2" not-null="true" /> </property> <property name="g" type="java.lang.Short"> <column name="G" /> </property> <property name="ab" type="short"> <column name="AB" not-null="true" /> </property> <property name="r" type="java.lang.Short"> <column name="R" /> </property> <property name="h" type="java.lang.Short"> <column name="H" /> </property> <property name="2b" type="java.lang.Short"> <column name="2B" /> </property> <property name="3b" type="java.lang.Short"> <column name="3B" /> </property> <property name="hr" type="short"> <column name="HR" not-null="true" /> </property> <property name="rbi" type="java.lang.Short"> <column name="RBI" /> </property> <property name="sb" type="java.lang.Short"> <column name="SB" /> </property> <property name="cs" type="java.lang.Short"> <column name="CS" /> </property> <property name="bb" type="java.lang.Short"> <column name="BB" /> </property> <property name="so" type="java.lang.Short"> <column name="SO" /> </property> <property name="ibb" type="java.lang.Short"> <column name="IBB" /> </property> <property name="hbp" type="java.lang.Short"> <column name="HBP" /> </property> <property name="sh" type="java.lang.Short"> <column name="SH" /> </property> <property name="sf" type="java.lang.Short"> <column name="SF" /> </property> <property name="gidp" type="java.lang.Short"> <column name="GIDP" /> </property> <!-- below is the association that I am trying to make --> <many-to-one name="master" column="playerID" entity-name="baseball.hibernate.Master" property-ref="playerId"
insert="false" update="false" /> </class> </hibernate-mapping>
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Generated Jul 18, 2009 3:03:12 PM by Hibernate Tools 3.2.2.GA --> <hibernate-mapping> <class name="baseball.hibernate.Master" table="master" catalog="baseball"> <id name="lahmanId" type="java.lang.Short"> <column name="lahmanID" /> <generator class="identity" /> </id> <property name="playerId" type="string"> <column name="playerID" length="10" unique="true" not-null="true" /> </property> <property name="managerId" type="string"> <column name="managerID" length="10" not-null="true" /> </property> <property name="hofId" type="string"> <column name="hofID" length="10" not-null="true" /> </property> <property name="birthYear" type="java.lang.Short"> <column name="birthYear" /> </property> <property name="birthMonth" type="java.lang.Short"> <column name="birthMonth" /> </property> <property name="birthDay" type="java.lang.Short"> <column name="birthDay" /> </property> <property name="birthCountry" type="string"> <column name="birthCountry" length="50" /> </property> <property name="birthState" type="string"> <column name="birthState" length="2" /> </property> <property name="birthCity" type="string"> <column name="birthCity" length="50" /> </property> <property name="deathYear" type="java.lang.Short"> <column name="deathYear" /> </property> <property name="deathMonth" type="java.lang.Short"> <column name="deathMonth" /> </property> <property name="deathDay" type="java.lang.Short"> <column name="deathDay" /> </property> <property name="deathCountry" type="string"> <column name="deathCountry" length="50" /> </property> <property name="deathState" type="string"> <column name="deathState" length="2" /> </property> <property name="deathCity" type="string"> <column name="deathCity" length="50" /> </property> <property name="nameFirst" type="string"> <column name="nameFirst" length="50" /> </property> <property name="nameLast" type="string"> <column name="nameLast" length="50" not-null="true" /> </property> <property name="nameNote" type="string"> <column name="nameNote" /> </property> <property name="nameGiven" type="string"> <column name="nameGiven" /> </property> <property name="nameNick" type="string"> <column name="nameNick" /> </property> <property name="weight" type="java.lang.Short"> <column name="weight" /> </property> <property name="height" type="java.lang.Double"> <column name="height" precision="4" scale="1" /> </property> <property name="bats" type="string"> <column name="bats" length="2" /> </property> <property name="throws_" type="string"> <column name="throws" length="2" /> </property> <!-- --> <property name="debut" type="java.sql.Date"> <column name="debut" /> </property> <property name="finale" type="java.sql.Date"> <column name="finaleGame" /> </property> <property name="college" type="string"> <column name="college" length="50" /> </property> <property name="lahman40id" type="string"> <column name="lahman40ID" length="9" /> </property> <property name="lahman45id" type="string"> <column name="lahman45ID" length="9" /> </property> <property name="retroId" type="string"> <column name="retroID" length="9" /> </property> <property name="holtzId" type="string"> <column name="holtzID" length="9" /> </property> <property name="bbrefId" type="string"> <column name="bbrefID" length="9" /> </property>
</class> </hibernate-mapping>
package baseball.hibernate;
// Generated May 31, 2008 6:03:13 PM by Hibernate Tools 3.2.1.GA
/** * Battingpost generated by hbm2java */ public class Battingpost implements java.io.Serializable {
private BattingpostId id; private Short yearId; private String round; private String playerId; private String teamId; private String lgId; private Short g; private Short ab; private Short r; private Short h; private Short twob; private Short threeb; private Short hr; private Short rbi; private Short sb; private Short cs; private Short bb; private Short so; private Short ibb; private Short hbp; private Short sh; private Short sf; private Short gidp; // private String nameFirst; // private String nameLast ; private Master master ; public Battingpost() { }
public Battingpost(Short yearId, String round, String playerId, String teamId, String lgId, Short g, Short ab, Short r, Short
h, Short twob, Short threeb, Short hr, Short rbi, Short sb, Short cs, Short bb, Short so, Short ibb, Short hbp, Short sh, Short
sf, Short gidp) { this.yearId = yearId; this.round = round; this.playerId = playerId; this.teamId = teamId; this.lgId = lgId; this.g = g; this.ab = ab; this.r = r; this.h = h; this.twob = twob; this.threeb = threeb; this.hr = hr; this.rbi = rbi; this.sb = sb; this.cs = cs; this.bb = bb; this.so = so; this.ibb = ibb; this.hbp = hbp; this.sh = sh; this.sf = sf; this.gidp = gidp; } /* */ public Master getMaster() { return this.master; }
public void setMaster(Master t) { this.master = t; } /* public java.lang.String getNameFirst() { return nameFirst; }
public void setNameFirst(java.lang.String nameFirst) { this.nameFirst = nameFirst; }
public java.lang.String getNameLast() { return nameLast; } public void setNameLast(java.lang.String nameLast) { this.nameLast = nameLast; } */ public BattingpostId getId() { return this.id; } public void setId(BattingpostId id) { this.id = id; } public Short getYearId() { return this.yearId; } public void setYearId(Short yearId) { this.yearId = yearId; } public String getRound() { return this.round; } public void setRound(String round) { this.round = round; } public String getPlayerId() { return this.playerId; } public void setPlayerId(String playerId) { this.playerId = playerId; } public String getTeamId() { return this.teamId; } public void setTeamId(String teamId) { this.teamId = teamId; } public String getLgId() { return this.lgId; } public void setLgId(String lgId) { this.lgId = lgId; } public Short getG() { return this.g; } public void setG(Short g) { this.g = g; } public Short getAb() { return this.ab; } public void setAb(Short ab) { this.ab = ab; } public Short getR() { return this.r; } public void setR(Short r) { this.r = r; } public Short getH() { return this.h; } public void setH(Short h) { this.h = h; } public Short get2b() { return this.twob; } public void set2b(Short twob) { this.twob = twob; } public Short get3b() { return this.threeb; } public void set3b(Short threeb) { this.threeb = threeb; } public Short getHr() { return this.hr; } public void setHr(Short hr) { this.hr = hr; } public Short getRbi() { return this.rbi; } public void setRbi(Short rbi) { this.rbi = rbi; } public Short getSb() { return this.sb; } public void setSb(Short sb) { this.sb = sb; } public Short getCs() { return this.cs; } public void setCs(Short cs) { this.cs = cs; } public Short getBb() { return this.bb; } public void setBb(Short bb) { this.bb = bb; } public Short getSo() { return this.so; } public void setSo(Short so) { this.so = so; } public Short getIbb() { return this.ibb; } public void setIbb(Short ibb) { this.ibb = ibb; } public Short getHbp() { return this.hbp; } public void setHbp(Short hbp) { this.hbp = hbp; } public Short getSh() { return this.sh; } public void setSh(Short sh) { this.sh = sh; } public Short getSf() { return this.sf; } public void setSf(Short sf) { this.sf = sf; } public Short getGidp() { return this.gidp; } public void setGidp(Short gidp) { this.gidp = gidp; }
}
package baseball.hibernate; // Generated May 31, 2008 6:03:13 PM by Hibernate Tools 3.2.1.GA
import java.sql.Date;;
/** * Master generated by hbm2java */ public class Master implements java.io.Serializable {
private Short lahmanId; private String playerId; private String managerId; private String hofId; private Short birthYear; private Short birthMonth; private Short birthDay; private String birthCountry; private String birthState; private String birthCity; private Short deathYear; private Short deathMonth; private Short deathDay; private String deathCountry; private String deathState; private String deathCity; private String nameFirst; private String nameLast; private String nameNote; private String nameGiven; private String nameNick; private Short weight; private Double height; private String bats; private String throws_; private java.sql.Date debut; private java.sql.Date finale; private String college; private String lahman40id; private String lahman45id; private String retroId; private String holtzId; private String bbrefId;
public Master() { }
public Master(Short lahmanId, String playerId, String managerId, String hofId, Short birthYear, Short birthMonth, Short
birthDay, String birthCountry, String birthState, String birthCity, Short deathYear, Short deathMonth, Short deathDay, String
deathCountry, String deathState, String deathCity, String nameFirst, String nameLast, String nameNote, String nameGiven, String
nameNick, Short weight, Double height, String bats, String thro, java.sql.Date debut, java.sql.Date finale, String college,
String lahman40id, String lahman45id, String retroId, String holtzId, String bbrefId) { this.lahmanId = lahmanId; this.playerId = playerId; this.managerId = managerId; this.hofId = hofId; this.birthYear = birthYear; this.birthMonth = birthMonth; this.birthDay = birthDay; this.birthCountry = birthCountry; this.birthState = birthState; this.birthCity = birthCity; this.deathYear = deathYear; this.deathMonth = deathMonth; this.deathDay = deathDay; this.deathCountry = deathCountry; this.deathState = deathState; this.deathCity = deathCity; this.nameFirst = nameFirst; this.nameLast = nameLast; this.nameNote = nameNote; this.nameGiven = nameGiven; this.nameNick = nameNick; this.weight = weight; this.height = height; this.bats = bats; this.throws_ = thro; this.debut = debut; this.finale = finale; this.college = college; this.lahman40id = lahman40id; this.lahman45id = lahman45id; this.retroId = retroId; this.holtzId = holtzId; this.bbrefId = bbrefId; } public Short getLahmanId() { return this.lahmanId; } public void setLahmanId(Short lahmanId) { this.lahmanId = lahmanId; } public String getPlayerId() { return this.playerId; } public void setPlayerId(String playerId) { this.playerId = playerId; } public String getManagerId() { return this.managerId; } public void setManagerId(String managerId) { this.managerId = managerId; } public String getHofId() { return this.hofId; } public void setHofId(String hofId) { this.hofId = hofId; } public Short getBirthYear() { return this.birthYear; } public void setBirthYear(Short birthYear) { this.birthYear = birthYear; } public Short getBirthMonth() { return this.birthMonth; } public void setBirthMonth(Short birthMonth) { this.birthMonth = birthMonth; } public Short getBirthDay() { return this.birthDay; } public void setBirthDay(Short birthDay) { this.birthDay = birthDay; } public String getBirthCountry() { return this.birthCountry; } public void setBirthCountry(String birthCountry) { this.birthCountry = birthCountry; } public String getBirthState() { return this.birthState; } public void setBirthState(String birthState) { this.birthState = birthState; } public String getBirthCity() { return this.birthCity; } public void setBirthCity(String birthCity) { this.birthCity = birthCity; } public Short getDeathYear() { return this.deathYear; } public void setDeathYear(Short deathYear) { this.deathYear = deathYear; } public Short getDeathMonth() { return this.deathMonth; } public void setDeathMonth(Short deathMonth) { this.deathMonth = deathMonth; } public Short getDeathDay() { return this.deathDay; } public void setDeathDay(Short deathDay) { this.deathDay = deathDay; } public String getDeathCountry() { return this.deathCountry; } public void setDeathCountry(String deathCountry) { this.deathCountry = deathCountry; } public String getDeathState() { return this.deathState; } public void setDeathState(String deathState) { this.deathState = deathState; } public String getDeathCity() { return this.deathCity; } public void setDeathCity(String deathCity) { this.deathCity = deathCity; } public String getNameFirst() { return this.nameFirst; } public void setNameFirst(String nameFirst) { this.nameFirst = nameFirst; } public String getNameLast() { return this.nameLast; } public void setNameLast(String nameLast) { this.nameLast = nameLast; } public String getNameNote() { return this.nameNote; } public void setNameNote(String nameNote) { this.nameNote = nameNote; } public String getNameGiven() { return this.nameGiven; } public void setNameGiven(String nameGiven) { this.nameGiven = nameGiven; } public String getNameNick() { return this.nameNick; } public void setNameNick(String nameNick) { this.nameNick = nameNick; } public Short getWeight() { return this.weight; } public void setWeight(Short weight) { this.weight = weight; } public Double getHeight() { return this.height; } public void setHeight(Double height) { this.height = height; } public String getBats() { return this.bats; } public void setBats(String bats) { this.bats = bats; } public String getThrows_() { return this.throws_; } public void setThrows_(String thro) { this.throws_ = thro; } public java.sql.Date getDebut() { return this.debut; } public void setDebut(java.sql.Date debut) { this.debut = debut; } public java.sql.Date getFinale() { return this.finale; } public void setFinale(java.sql.Date finale) { this.finale = finale; } public String getCollege() { return this.college; } public void setCollege(String college) { this.college = college; } public String getLahman40id() { return this.lahman40id; } public void setLahman40id(String lahman40id) { this.lahman40id = lahman40id; } public String getLahman45id() { return this.lahman45id; } public void setLahman45id(String lahman45id) { this.lahman45id = lahman45id; } public String getRetroId() { return this.retroId; } public void setRetroId(String retroId) { this.retroId = retroId; } public String getHoltzId() { return this.holtzId; } public void setHoltzId(String holtzId) { this.holtzId = holtzId; } public String getBbrefId() { return this.bbrefId; } public void setBbrefId(String bbrefId) { this.bbrefId = bbrefId; }
}
|