views:

34

answers:

2

I have this DB design:

CREATE TABLE report (
    ID          MEDIUMINT PRIMARY KEY NOT NULL AUTO_INCREMENT,
    user        MEDIUMINT NOT NULL,
    created     TIMESTAMP NOT NULL,
    state       INT NOT NULL,
    FOREIGN KEY (user) REFERENCES user(ID) ON UPDATE CASCADE ON DELETE CASCADE
);


CREATE TABLE reportProperties (
    ID          MEDIUMINT NOT NULL, 
    k           VARCHAR(128) NOT NULL,
    v           TEXT NOT NULL,
    PRIMARY KEY(
        ID, k
    ),
    FOREIGN KEY (ID) REFERENCES report(ID) ON UPDATE CASCADE ON DELETE CASCADE
);

and this Hibernate Markup:

@Table(name="report")
@Entity(name="ReportEntity")
public class ReportEntity extends Report{
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name="ID")
    private Integer ID;
    @Column(name="user")
    private Integer user;
    @Column(name="created")
    private Timestamp created;
    @Column(name="state")
    private Integer state = ReportState.RUNNING.getLevel();

    @OneToMany(mappedBy="pk.ID", fetch=FetchType.EAGER)
    @JoinColumns(
            @JoinColumn(name="ID", referencedColumnName="ID")
    )
    @MapKey(name="pk.key")
    private Map<String, ReportPropertyEntity> reportProperties = new HashMap<String, ReportPropertyEntity>();
}

and

@Table(name="reportProperties")
@Entity(name="ReportPropertyEntity")
public class ReportPropertyEntity extends ReportProperty{

    @Embeddable
    public static class ReportPropertyEntityPk implements Serializable{
        /**
         * long#serialVersionUID
         */
        private static final long serialVersionUID = 2545373078182672152L;
        @Column(name="ID")
        protected int ID;
        @Column(name="k")
        protected String key;
    }

    @EmbeddedId
    protected ReportPropertyEntityPk pk = new ReportPropertyEntityPk();

    @Column(name="v")
    protected String value;
}

And i have inserted on Report and 4 Properties for that report. Now when i execute this:

this.findByCriteria(
                        Order.asc("created"), 
                        Restrictions.eq("user", user.getObject(UserField.ID))
                    )
                );

I get back the report 4 times, instead of just the once with a Map with the 4 properties in. I'm not great at Hibernate to be honest, prefer straight SQL but I must learn, but i can't see what it is that is wrong.....?

Any suggestions?

A: 

i do not know the answer to your question, but you could do this another way and do away with that whole ReportPropertyEntity class :

@CollectionOfElements
@JoinTable(name = "reportProperties", joinColumns = { @JoinColumn(name = "id") })
@MapKey(columns = { @Column(name = "`key`") })
@Column(name = "`value`", length = 131070, nullable = false)
private Map<String, String> reportProperties = new HashMap<String, String>();
oedo
it might not have been what i though the answer would be, but works a treat. I need to swot up on hibernate, would make my life easier...thank you!
Andy
You should use @ElementCollection. See in documentation: "NoteWe recommend you to migrate from @org.hibernate.annotations.CollectionOfElements to the new @ElementCollection annotation."
Odelya
A: 

Try using this on your criteria/query:

setResultTransformer(DistinctRootEntityResultTransformer.INSTANCE);

This will work for all your tables. No need to modify mappings.

Kango_V