我正在使用@Multitenant(SINGLE_TABLE)来支持多租户。为解决n + 1问题,我使用了eclipselink的批量获取功能。
df = pd.DataFrame(data={'ev_type': np.random.choice(list("ABCDWK"), size=100,replace=True), 'ev_time': pd.date_range(start=pd.datetime(2016,1,1),freq='M', periods=100)})
当我使用:
查询时@Entity
public class TestEntity implements Serializable {
@Id
@Column
private Long id;
@OneToMany(mappedBy = "testEntity", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@BatchFetch(BatchFetchType.IN)
private List<TestEntityLine> lines = new ArrayList<>();
}
@Entity
public class TestEntityLine implements Serializable {
@Id
@Column
private Long id;
@JoinColumn(name = "PID", referencedColumnName = "ID", nullable = true)
private TestEntity testEntity;
}
sql和结果日志:
String boql = "SELECT e FROM TestEntity e order by e.id";
query.setFirstResult(1); // for pagination
query.setMaxResults(3); // for pagination
所有这些都很完美。 但是当我启用多租户时:
2016-06-16 10:05:14.558 [main] DEBUG o.e.p.s./.sql - SELECT ID AS a1 FROM TESTENTITY ORDER BY ID LIMIT ? OFFSET ?
bind => [3, 1]
entity-2
2016-06-16 10:05:14.594 [main] DEBUG o.e.p.s./.sql - SELECT ID, NAME, VALUE, PID FROM TESTENTITYLINE WHERE (PID IN (?,?,?))
bind => [2, 3, 4]
entityLine-2-3
entityLine-2-1
entityLine-2-2
entity-3
entityLine-3-2
entityLine-3-1
entityLine-3-3
entity-4
entityLine-4-3
entityLine-4-2
entityLine-4-1
当我使用:
查询时String boql =&#34; SELECT e FROM TestEntity e order by e.id&#34 ;; query.setFirstResult(1); //用于分页 query.setMaxResults(3); //用于分页
sql和结果日志:
@Entity
public class TestMultiTenantEntity implements Serializable {
@Id
@Column
private Long id;
@OneToMany(mappedBy = "testEntity", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@BatchFetch(BatchFetchType.IN)
private List<TestMultiTenantEntityLine> lines = new ArrayList<>();
}
@Entity
public class TestMultiTenantEntityLine implements Serializable {
@Id
@Column
private Long id;
@JoinColumn(name = "PID", referencedColumnName = "ID", nullable = true)
private TestMultiTenantEntity testEntity;
}
@MappedSuperclass
@Multitenant(SINGLE_TABLE)
@TenantDiscriminatorColumn(discriminatorType = DiscriminatorType.INTEGER, primaryKey = true, name = MultiTenantSupport.TENANT_COLUMN_NAME, length = 12, contextProperty = MultiTenantSupport.MULTITENANT_CONTEXT_PROPERTY)
public abstract class MultiTenantSupport {
public static final String MULTITENANT_CONTEXT_PROPERTY = "tenant_id";
public static final String TENANT_COLUMN_NAME = "TENANT_ID";
}
为什么TESTMULTITENANTENTITYLINE会自行加入t0 / t1?这会产生许多重复的记录
我尝试获取记录2,3,4,为什么它取出1,5,6?
你可以帮忙吗?这是一个错误吗?或者我错过了什么?提前谢谢。答案 0 :(得分:1)
通过以下设置从主键中删除tenant_id后,它可以正常工作:
@TenantDiscriminatorColumn(primaryKey = false, ...)
但是,尽管我不需要它们,但是JOIN和EXIST选项不起作用。 它看起来在JOIN和EXIST sql中所需的pid不包含在where条件中。这就是为什么它加载所有其他不需要的条目