0

I am trying to view properties of a child table (BeanPropertyMetaData) after querying a the parent table (BeanMetaData). I am unable to see the output of the child table values on the console. I don't get any exception or error msgs either. Can you please help me identify where I am going wrong. I am using Hiberate 3 with spring 3. The source code is as follows:

The parent class:

@Entity 
@Table(name="BEAN_META_DATA")
public class BeanMetaData extends GeneralEntity {

private String beanName;
private String mappedTable;
private String title;
private List<BeanPropertyMetaData> properties;

@Id
@Column(name = "D_PK", nullable = false, precision = 22, scale = 0)
@TableGenerator( name = "appSeqStore", table = "APP_SEQ_STORE", pkColumnName = "APP_SEQ_NAME", pkColumnValue = "BEAN_PK", valueColumnName = "APP_SEQ_VALUE", initialValue = 1, allocationSize = 1 )
@GeneratedValue( strategy = GenerationType.TABLE, generator = "appSeqStore" )
public long getPk() {
    return pk;
}

@Column(name = "BEAN_NAME", nullable = false, length=60)
public String getBeanName() {
    return beanName;
}
public void setBeanName(String beanName) {
    this.beanName = beanName;
}

@Column(name = "TABLENAME", nullable = false, length=60)
public String getMappedTable() {
    return mappedTable;
}
public void setMappedTable(String mappedTable) {
    this.mappedTable = mappedTable;
}

@Column(name = "TITLE", nullable = false, length=60)
public String getTitle() {
    return title;
}
public void setTitle(String title) {
    this.title = title;
}


**@OneToMany(mappedBy="beanName",cascade = { javax.persistence.CascadeType.ALL}, fetch = FetchType.LAZY)
public List<BeanPropertyMetaData> getProperties() {
    return properties;
}**

public void setProperties(List<BeanPropertyMetaData> props) {
    this.properties = props;
} 

}

The child class:

@Entity
@Table(name="BEAN_PROPERTY_DATA")
public class BeanPropertyMetaData extends GeneralEntity {

String propertyName;
String mappedColumn;
DataType dataType;
String label;
boolean isUnique;
boolean isMandatory;
boolean showInList;
int listSeq;
int listColLength;
BeanMetaData beanName;
BeanPropertyMetaData refBean;


@Id
@Column(name = "D_PK", nullable = false, precision = 22, scale = 0)
@TableGenerator( name = "appSeqStore", table = "APP_SEQ_STORE", pkColumnName = "APP_SEQ_NAME", pkColumnValue = "BEAN_PROPERTY_PK", valueColumnName = "APP_SEQ_VALUE", initialValue = 1, allocationSize = 1 )
@GeneratedValue( strategy = GenerationType.TABLE, generator = "appSeqStore" )
public long getPk() {
    return pk;
}

@Column(name = "PROPERTY_NAME", nullable = false, length=60)
public String getPropertyName() {
    return propertyName;
}
public void setPropertyName(String propertyName) {
    this.propertyName = propertyName;
}

@Column(name = "COLUMN_NAME", length=60)
public String getMappedColumn() {
    return mappedColumn;
}

public void setMappedColumn(String mappedColumn) {
    this.mappedColumn = mappedColumn;
}

@Column(name = "DATATYPE", length=60)
@Enumerated(EnumType.STRING)
public DataType getDataType() {
    return dataType;
}
public void setDataType(DataType dataType) {
    this.dataType = dataType;
}

@Column(name = "LABEL", length=60)
public String getLabel() {
    return label;
}
public void setLabel(String label) {
    this.label = label;
}

@Column(name = "IS_UNIQUE")
public boolean isUnique() {
    return isUnique;
}
public void setUnique(boolean isUnique) {
    this.isUnique = isUnique;
}

@Column(name = "IS_NULL")
public boolean isMandatory() {
    return isMandatory;
}
public void setMandatory(boolean isMandatory) {
    this.isMandatory = isMandatory;
}

@Column(name = "IN_LIST")
public boolean isShowInList() {
    return showInList;
}
public void setShowInList(boolean showInList) {
    this.showInList = showInList;
}

@Column(name = "LIST_SEQ",precision = 22, scale = 0 )
public int getListSeq() {
    return listSeq;
}
public void setListSeq(int listSeq) {
    this.listSeq = listSeq;
}

@Column(name = "LIST_LENGTH", precision = 22, scale = 0)
public int getListColLength() {
    return listColLength;
}
public void setListColLength(int listColLength) {
    this.listColLength = listColLength;
}

**@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "FK_BEAN_META_DATA", nullable = false)
public BeanMetaData getBeanName() {
    return beanName;
}**
public void setBeanName(BeanMetaData beanName) {
    this.beanName = beanName;
}

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "FK_BEAN_PROPERTY")
public BeanPropertyMetaData getRefBean() {
    return refBean;
}
public void setRefBean(BeanPropertyMetaData refBean) {
    this.refBean = refBean;
}

}

I have populated values in both tables in the database. I am running the following test:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:spring-db-config.xml", "classpath:spring-beans.xml"})
public class BeanServiceTest {

@Autowired 
DatabaseDAO databaseDAO;

@Test
public void getBeanMetaData(){
    String beanName= "com.inspireme.ebeans.Project";
    String query= "from BeanMetaData as metadata where beanName= '"+beanName+"'";
    System.out.println("1");
    SessionFactory sf= databaseDAO.getSessionFactory();
    System.out.println("2");
    //Session session = sf.getCurrentSession();
    Session session = sf.openSession();
    System.out.println("3");
    session.beginTransaction();
    System.out.println("4");
    Query hibQuery= session.createQuery(query);
    System.out.println("5");
    List result= hibQuery.list();
    System.out.println("6");
    BeanMetaData beanData= (BeanMetaData) result.get(0);
    System.out.println("7");
    System.out.println("bean data= "+beanData.getTitle()+", pk= "+beanData.getPk());
    System.out.println("9");
    List<BeanPropertyMetaData> properties= beanData.getProperties();
    System.out.println("10");
    System.out.println("prop size= "+properties.size());
    System.out.println("11");
    for(BeanPropertyMetaData prop: properties){
        System.out.println("prop name= "+prop.getLabel());

    }
    session.close();
    assertTrue(true);
}

I get the following output on the console:

0 [main] INFO com.inspireme.utils.PropertiesUtil - Loading properties file from class path resource [database.properties]
1
2
3
4
5
Hibernate: select beanmetada0_.D_PK ...
6
7
bean data= Project, pk= 2
9
10
Hibernate: select properties0_.FK_BEAN_META_DATA asFK15_2_, properties0_.D_PK as D1_2_, properties0_.D_PK as D1_6_1_, properties0_.D_CREATED_BY as D2_6_1_, properties0_.D_CREATED as D3_6_1_, properties0_.D_MODIFIED_BY as D4_6_1_, properties0_.D_MODIFIED as D5_6_1_, properties0_.FK_BEAN_META_DATA as FK15_6_1_, properties0_.DATATYPE as DATATYPE6_1_, properties0_.LABEL as LABEL6_1_, properties0_.LIST_LENGTH as LIST8_6_1_, properties0_.LIST_SEQ as LIST9_6_1_, properties0_.IS_NULL as IS10_6_1_, properties0_.COLUMN_NAME as COLUMN11_6_1_, properties0_.PROPERTY_NAME as PROPERTY12_6_1_, properties0_.FK_BEAN_PROPERTY as FK16_6_1_, properties0_.IN_LIST as IN13_6_1_, properties0_.IS_UNIQUE as IS14_6_1_, beanproper1_.D_PK as D1_6_0_, beanproper1_.D_CREATED_BY as D2_6_0_, beanproper1_.D_CREATED as D3_6_0_, beanproper1_.D_MODIFIED_BY as D4_6_0_, beanproper1_.D_MODIFIED as D5_6_0_, beanproper1_.FK_BEAN_META_DATA as FK15_6_0_, beanproper1_.DATATYPE as DATATYPE6_0_, beanproper1_.LABEL as LABEL6_0_, beanproper1_.LIST_LENGTH as LIST8_6_0_, beanproper1_.LIST_SEQ as LIST9_6_0_, beanproper1_.IS_NULL as IS10_6_0_, beanproper1_.COLUMN_NAME as COLUMN11_6_0_, beanproper1_.PROPERTY_NAME as PROPERTY12_6_0_, beanproper1_.FK_BEAN_PROPERTY as FK16_6_0_, beanproper1_.IN_LIST as IN13_6_0_, beanproper1_.IS_UNIQUE as IS14_6_0_ from BEAN_PROPERTY_DATA properties0_ left outer join BEAN_PROPERTY_DATA beanproper1_ on properties0_.FK_BEAN_PROPERTY=beanproper1_.D_PK where properties0_.FK_BEAN_META_DATA=?

Why the program not printing the list size? Any help is greatly appreciated.

Francisco Puga
  • 23,869
  • 5
  • 48
  • 64
parthiv108
  • 56
  • 1
  • Table Generator is not required at this point.. can u check without it ? – Balaji Reddy Mar 14 '14 at 09:44
  • I think you need fetchType eager to do what you want. Here is a good answer that provides more detail: http://stackoverflow.com/questions/2990799/difference-between-fetchtype-lazy-and-eager-in-java-persistence – user3360944 Mar 14 '14 at 13:07
  • I just realised that I added data manually directly into the database. I will persist some data through Hibernate APIs and try reading. – parthiv108 Mar 14 '14 at 21:26
  • Ok the problem was resolved by inserting data thru hibernate before reading it. – parthiv108 Mar 18 '14 at 04:42

0 Answers0