javabyte-buddy

BiDirectional Classes Association using ByteBuddy


I am mitigating a problem related on how to generate a bidirectional association in Java using DynamicTypes provided by ByteBuddy.

My main concern is how to define such DynamicTypes, investigating the samples and other questions I haven't found anything related about the topic, I read something about TypeCache, but still didn't figure out how it could work.

Any direction will be a great help even if it's not possible to generate DynamicTypes with a birectional association.

A Sample code for the future generated types" provided as example follows

public class SimpleTableEntity 
implements Base<SimpleTableEntity> {

    @Id
    @Column(name="simple_key" , nullable = false)
    private String simpleKey;

    @Column(name="simple_column" , nullable = false)
    private String simpleColumn;

    @Column(name="simple_date" , nullable = false)
    private Date simpleDate;

    @Column(name="simple_timestamp" , nullable = false)
    private Timestamp simpleTimestamp;
    
    @OneToMany(mappedBy = "simpleTableEntity", cascade = CascadeType.ALL)
    private Set<SimpleForeignTableEntity> simpleForeignTableEntitySet;
    }

public class SimpleForeignTableEntity {

    @Id
    @Column(name="simple_foreign_key",nullable=false)
    private String simpleForeignKey;
    
    @Column(name="simple_foreign_column",nullable=false)
    private String simpleForeignColumn;
    
    @ManyToOne
    @JoinColumn(name = "simple_key",nullable = false)
    private SimpleTableEntity simpleTableEntity;
    }

Solution

  • You can always define one type and then create a TypeDescription from a DynamicType.Builder which you can still adjust later. Also, you can create a TypeDescription.Latent as a placeholder.