简体   繁体   中英

Composite foreign key and DDL generation in JPA 2

I'm trying to create a class with a foreign composite key and everything works fine, except that the DDL generation doesn't create indeces as expected.

I will explain better with some code; the part of the code I'm trying to implement is the ribbon awarding: a User can have many Ribbon s and a Ribbon can have many User s (many-to-many).

Ribbon class:

@Entity
@Table(name = "ribbon")
public class Ribbon
{
    @AttributeOverrides({
            @AttributeOverride(
                    name = "id",
                    column = @Column(name = "id", nullable = false, length = 4)
            ),
            @AttributeOverride(
                    name = "level",
                    column = @Column(name = "level", nullable = false)
            )
    })
    @EmbeddedId
    RibbonId id;

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true, mappedBy = "ribbon")
    private Set<UserHasRibbon> userHasRibbonSet;

    // ...
}

RibbonId class:

@Embeddable
public class RibbonId implements Serializable
{
    public int id;

    @Enumerated(EnumType.STRING)
    public UserLevel level;

    @Override
    public boolean equals(Object o)
    {
        // ...
    }

    @Override
    public int hashCode()
    {
        // ...
    }
}

UserHasRibbon class:

@Entity
@Table(name = "user_has_ribbon")
public class UserHasRibbon
{
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id", nullable = false, length = 16)
    private int id;

    @ManyToOne(optional = false)
    @JoinColumn(name = "user_id", nullable = false)
    private User user;

    @ManyToOne(optional = false)
    @JoinColumns({
            @JoinColumn(name="ribbon_id", referencedColumnName="id", nullable = false),
            @JoinColumn(name="ribbon_level", referencedColumnName="level", nullable = false)
    })
    private Ribbon ribbon;

    @Column(name = "earned_date", nullable = false)
    @Temporal(TemporalType.TIMESTAMP)
    private Date earnedDate;

    // ...
}

It's pretty straightforward and everything is working perfectly. The only problem is that I noticed that the automatic DDL generation didn't create the indeces in the user_has_ribbon table:

  • ribbon_id has an index but is not linked to ribbon . id
  • ribbon_level doesn't have an index at all
  • user_id is correct, has an index linked to user . id

I know that the DDL generation should be disabled for production, I'm just wondering why it's not behaving as expected.

Thank you!

I just realized that JPA did create a composite index on the two columns ( ribbon_id and ribbon_level ) and it's working fine. I got confused because phpMyAdmin doesn't show relations correctly when it's about composite indeces...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM