On 01/14/2011 02:45 PM, Ondřej Fafejta wrote:

Nakoniec to poriesilo:

@Embeddable
public class BundledMaterialPK implements Serializable{

    @Column(name="bundle_id")
    private Long bundleID;

    @Column(name="material_id")
    private Long materialID;
}

a

@Entity
public class BundledMaterial implements Serializable {

    @EmbeddedId
    private BundledMaterialPK id;

    @ManyToOne()
@JoinColumn(name = "material_id", insertable = false, updatable = false)
    private Material material;

    @ManyToOne()
    @JoinColumn(name = "bundle_id", insertable = false, updatable = false)
    private Bundle bundle;

}

Diky za nakop.


--
Dusan

Zdravím,

pokud si pamatuju, tak se to řeší následovně:

public class BundledMaterialPK implements Serializable {

   @Column(name="material_id")
    private Long materialId;

    @Column(name="bundle_id")
    private Long bundleId;

tzn. ten primární klíč neobsahuje @Embeddable a neobsahuje odkazy na
objekty @OneToOne, ale přímo Long ...

entita BundledMaterial pak vypadá takto

@Entity
@IdClass(BundledMaterialPK.class)
public class BundledMaterial implements Serializable {

@Id
private Long materialId;

@Id
private Long bundleId;

// a hlavne

@ManyToOne()
@JoinColumn(name = "material_id", insertable = false, updatable = false)
private Material material;



Píšu to z hlavy, můžou tam být chyby.
V každém případě je důležité, že se pro třídu PK používají primitivní
typy (long, int)
V hlavní entitě se pak přidá odkaz na druhou entitu pomocí @ManyToOne,
kde musí být nastaveno insertable a updateable na false.

Je možné použít i druhý způsob s Embeddable viz.
http://stackoverflow.com/questions/3669883/hibernate-where-do-insertable-false-updatable-false-belong-in-composite-pri

Fafi


2011/1/14 msk.conf<[email protected]>:
  Ahoj.

Narazil som na podivny problem. Po uprave entit v existujucom projekte (
zmena modelu
vyziadala pouzivanie composite keys ) sa hibernate snazi ako value
kompozitneho kluca
serializovat objekt do blobu ( co asi neni to co chcem ).

Entity vyzeraju nejak takto:

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public class Material implements Serializable {

    @Id
    @GeneratedValue ...
    private Long material_id;
  ...
}

@Entity
public class BundledMaterial implements Serializable {

    @EmbeddedId
    private BundledMaterialPK id;        // pouzivam zlozeny kluc
bundle:material
    private Long amount;
  ...
}

@Embeddable
public class BundledMaterialPK implements Serializable {

    @OneToOne(mappedBy = "material_id")
    private Material material;

    @OneToOne(mappedBy = "bundle_id")
    private Bundle bundle;
  ...
}


@Entity
public class Bundle implements Serializable {

    @Id
    @GeneratedValue ...
    private Long bundle_id;

    @OneToMany(mappedBy="id.bundle", cascade=CascadeType.ALL)
    private List<BundledMaterial>  bundledMaterial = new
ArrayList<BundledMaterial>();
  ...
}


V principe ide o to, ze mam nejaky zoznam materialu, zoznam bundlov, kazdy
bundle obsahuje
mnozinu materialu, kazdy v roznom mnozstve pre kazdy bundle.

Schemu generuje hibernate, pricom logicky vygeneruje nasledovne tabule:

material ( material_id PK, ... )
bundle ( bundle_id PK, ... )

ale uplne nelogicky vygeneruje tabulku BundledMaterial:

bundledmaterial (
  bundle blob (?),
  material blob (?),
  amount long,
  primary key ( bundle, amount )
)

a snazi sa BundledMaterialPK.material a BundledMaterialPK.bundle do to tych
blobov hexaserializovat.

Netusi niekto na co som zabudol, pripadne co som spravil zle?

Diky

--
Dusan




Odpovedet emailem