eclipselink MySQL左连接错误:未知列

时间:2013-01-15 11:29:06

标签: mysql jpa-2.0 eclipselink left-join

我有这个数据模型:

AbstractEntity (abstract, @MappedSuperClass)
    |
    +---- Subject (abstract, @Entity, joined)
    |       |
    |       +---- Person (@Entity)
    |       |
    |       +---- ...
    |
    +---- Metadata (abstract, @Entity, joined)
            |
            +---- ...

意识到:

@MappedSuperclass
public abstract class AbstractEntity implements Serializable
{
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(nullable = false)
    protected Long id;

    @Version
    protected Integer version;

    ...
}

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class Subject extends AbstractEntity
{
    @ManyToOne
    @JoinColumn(name = "PROFILE_ID")
    protected Profile profile;

    @ElementCollection
    @CollectionTable(name = "SUBJECT_RIGHTS", joinColumns = @JoinColumn(name = "OWNER_ID"))
    @Column(name = "CODE")
    protected Set<String> rightSet = new HashSet<String>();

    @Transient
    public abstract String getTitle();

    ...
}

@Entity
public class Person extends AbstractEntity
{
    @NotBlank
    private String firstName;

    @NotBlank
    private String lastName;

    ...
}

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class Metadata extends AbstractEntity
{
    @ElementCollection
    @CollectionTable(name = "METADATA_PREFERENCE", joinColumns = @JoinColumn(name = "METADATA_ID"))
    @MapKeyJoinColumn(name = "PERSON_ID", nullable = false)
    @Enumerated(EnumType.STRING)
    @Column(name = "PREFERENCE", nullable = false)
    protected Map<Person, PreferenceType> preferenceMap = new LinkedHashMap<Person, PreferenceType>();

    ...
}

我试图执行此查询:

select m.id from Metadata m left join m.preferenceMap p [where not important]

和eclipselink生成此SQL:

SELECT t0.ID FROM METADATA t0 LEFT OUTER JOIN METADATA_PREFERENCE t3 ON ((t3.METADATA_ID = t0.ID) AND (t1.ID = t3.PERSON_ID)), PERSON t2, SUBJECT t1 [where not important]

但是此查询会生成异常:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 't1.ID' in 'on clause'

我知道MySQL加入优先级,所以我需要eclipselink以这种方式重写查询:

SELECT t0.ID FROM (METADATA t0, SUBJECT t1) LEFT OUTER JOIN METADATA_PREFERENCE t3 ON ((t3.METADATA_ID = t0.ID) AND (t1.ID = t3.PERSON_ID)), PERSON t2 [where not important]

怎么做???

然而我正在使用:

mysql:
+-------------------------+------------------------------+
| Variable_name           | Value                        |
+-------------------------+------------------------------+
| innodb_version          | 1.1.7                        |
| protocol_version        | 10                           |
| slave_type_conversions  |                              |
| version                 | 5.5.13-log                   |
| version_comment         | MySQL Community Server (GPL) |
| version_compile_machine | x86                          |
| version_compile_os      | Win64                        |
+-------------------------+------------------------------+

eclipselink: 
Eclipse Persistence Services - 2.4.1.v20121003-ad44345

这是persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
    <persistence-unit name="prime" transaction-type="JTA">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <jta-data-source>jdbc/prime</jta-data-source>
        <exclude-unlisted-classes>false</exclude-unlisted-classes>
        <properties>
            <property name="eclipselink.target-database" value="MySQL"/>
            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/prime"/>
            <property name="javax.persistence.jdbc.user" value="root"/>
            <property name="javax.persistence.jdbc.password" value="password"/>
        </properties>
    </persistence-unit>
</persistence>

2 个答案:

答案 0 :(得分:0)

似乎是因为加入复杂的Map ElementCollection而感到困惑。似乎是一个错误,请记录错误并为其投票,同时确保您使用最新的补丁版本。

您可以将ElementCollection作为OneToMany映射到具有人物和类型的Preference对象。这将是一个更直接的模式。

如果您不需要外部联接(或者如果需要外部联接语义,则可能使用子查询),内联接也可能有效。

答案 1 :(得分:0)

eclipselink 2.5.0(jpa 2.1)解决了这个错误。