Hibernate arrays?
i have a POJO and a hibernate that holds integers that specify the points system for a particular race(i.e 1st place = 100, 2nd place = 70......) and was wondering if their is a way to create an array object and map it in my hbm file and pojo class.
here is how it looks at the moment:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.jr.model.RaceSeriesPointsRules" table="RACE_SERIES_POINTS_RULES">
<id name="id" type="string">
<column name="ID" />
<generator class="uuid" />
</id>
<property name="pos1" type="integer" column="POS1" />
<property name="pos2" type="integer" column="POS2" />
<property name="pos3" type="integer" column="POS3" />
<property name="pos4" type="integer" column="POS54" />
<property name="pos5" type="integer" column="POS5" />
<property name="pos6" type="integer" column="POS6" />
<property name="pos7" type="integer" column="POS7" />
<property name="pos8" type="integer" column="POS8" />
<property name="pos9" type="integer" column="POS9" />
<property name="pos10" type="integer" column="POS10" />
<property name="pos11" type="integer" column="POS11" />
<property name="pos12" type="integer" column="POS12" />
<property name="pos13" type="integer" column="POS13" />
<property name="pos14" type="integer" column="POS14" />
<property name="pos15" type="integer" column="POS15" />
<property name="pos16" type="integer" column="POS16" />
</class>
</hibernate-mapping>
Here is my pojo below:
package com.jr.model;
public class RaceSeriesPointsRules {
private String id;
private int pos1;
private int pos2;
private int pos3;
private int pos4;
private int pos5;
private int pos6;
private int pos7;
private int pos8;
private int pos9;
private int pos10;
private int pos11;
private int pos12;
private int pos13;
private int pos14;
private int pos15;
private int pos16;
public RaceSeriesPointsRules(int pos1, int pos2, int pos3, int pos4,
int pos5, int pos6, int pos7, int pos8, int pos9, int pos10,
int pos11, int pos12, int pos13, int pos14, int pos15, int pos16) {
this.pos1 = pos1;
this.pos2 = pos2;
this.pos3 = pos3;
this.pos4 = pos4;
this.pos5 = pos5;
this.pos6 = pos6;
this.pos7 = pos7;
this.pos8 = pos8;
this.pos9 = pos9;
this.pos10 = pos10;
this.pos11 = pos11;
this.pos12 = pos12;
this.pos13 = pos13;
this.pos14 = pos14;
this.pos15 = pos15;
this.pos16 = pos16;
}
public RaceSeriesPointsRules(){
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public int getPos1() {
return pos1;
}
public void setPos1(int pos1) {
this.pos1 = pos1;
}
public int getPos2() {
return pos2;
}
public void setPos2(int pos2) {
this.pos2 = pos2;
}
public int getPos3() {
return pos3;
}
public void setPos3(int pos3) {
this.pos3 = pos3;
}
public int getPos4() {
return pos4;
}
public void setPos4(int pos4) {
this.pos4 = pos4;
}
public int getPos5() {
return pos5;
}
public void setPos5(int pos5) {
this.pos5 = pos5;
}
public int getPos6() {
return pos6;
}
public void setPos6(int pos6) {
this.pos6 = pos6;
}
public int getPos7() {
return pos7;
}
public void setPos7(int pos7) {
this.pos7 = pos7;
}
public int getPos8() {
return pos8;
}
public void setPos8(int pos8) {
this.pos8 = pos8;
}
public int getPos9() {
return pos9;
}
public void setPos9(int pos9) {
this.pos9 = pos9;
}
public int getPos10() {
return pos10;
}
public void setPos10(int pos10) {
this.pos10 = pos10;
}
public int getPos11() {
return pos11;
}
public void setPos11(int pos11) {
this.pos11 = pos11;
}
public int getPos12() {
return pos12;
}
public void setPos12(int pos12) {
this.pos12 = pos12;
}
public int getPos13() {
return pos13;
}
public void setPos13(int pos13) {
this.pos13 = pos13;
}
public int getPos14() {
return pos14;
}
public void setPos14(int pos14) {
this.pos14 = pos14;
}
public int getPos15() {
return pos15;
}
public void setPos15(int pos15) {
this.pos15 = pos15;
}
public int getPos16() {
return pos16;
}
public void setPos16(int pos16) {
this.pos16 = pos16;
}
}
As you can se开发者_JAVA百科e its a hack job and would like to know if i can use arrays in hibernate to create columns of array.size and populate them with ints. this will make my pojo very small and do logic in calculating the pts that a racer has accumulated based on the position they finished:
racer A finishes 2nd, racer a position = 2;
Select column.pos2 from race_series_points_rules where .........
edit: another question is what happens if race series A has only 10 positions and Race Series B has 16? can both raceSeriesPointsRules be stores in the same table even though both rows will have different column count?
Hope my explanations are clear. Thanks in advance.
I would use an indexed collection for that, maybe even doing your own implementation of List (delegating all methods to the actual implementation, but allowing only 10 elements). This way, you make sure to have at most 10 items, and you'll still keep their positions. You can also easily calculate the points by looking at the index.
http://docs.jboss.org/hibernate/core/3.3/reference/en/html/collections.html#collections-indexed
精彩评论