eclipselink batch fetch IN does not work well with multiple tenant - eclipselink

I am using #Multitenant(SINGLE_TABLE) to support multi-tenancy. To fix the n+1 problem, I use batch fetch feature of eclipselink.
#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;
}
When I query using:
String boql = "SELECT e FROM TestEntity e order by e.id";
query.setFirstResult(1); // for pagination
query.setMaxResults(3); // for pagination
The sql and results log:
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
All of these works perfectly.
But when I enable multi-tenant:
#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";
}
When I query using:
String boql = "SELECT e FROM TestEntity e order by e.id";
query.setFirstResult(1); // for pagination
query.setMaxResults(3); // for pagination
The sql and results log:
2016-06-16 10:17:17.123 [main] DEBUG o.e.p.s./.sql - SELECT TENANT_ID AS a1, ID AS a2 FROM TESTMULTITENANTENTITY WHERE (TENANT_ID = ?) ORDER BY ID LIMIT ? OFFSET ?
bind => [1, 3, 1]
entity-2
2016-06-16 10:17:17.159 [main] DEBUG o.e.p.s./.sql - SELECT t0.TENANT_ID, t0.ID, t0.NAME, t0.VALUE, t0.PID FROM TESTMULTITENANTENTITYLINE t0, TESTMULTITENANTENTITYLINE t1 WHERE ((((t1.TENANT_ID, t1.PID) IN ((?,?),(?,?),(?,?))) AND (t0.TENANT_ID = ?)) AND (t1.TENANT_ID = ?))
bind => [1, 2, 1, 3, 1, 4, 1, 1] **?????? Why TESTMULTITENANTENTITYLINE joins to itself t0/t1? This produce many duplicated records**
2016-06-16 10:17:17.227 [main] DEBUG o.e.p.s./.sql - SELECT TENANT_ID, ID FROM TESTMULTITENANTENTITY WHERE (((TENANT_ID = ?) AND (ID = ?)) AND (TENANT_ID = ?))
bind => [1, 1, 1] **?????? I try to fetch record 2,3,4, why it fetch out 1, 5, 6?**
2016-06-16 10:17:17.267 [main] DEBUG o.e.p.s./.sql - SELECT TENANT_ID, ID FROM TESTMULTITENANTENTITY WHERE (((TENANT_ID = ?) AND (ID = ?)) AND (TENANT_ID = ?))
bind => [1, 5, 1]
2016-06-16 10:17:17.295 [main] DEBUG o.e.p.s./.sql - SELECT TENANT_ID, ID FROM TESTMULTITENANTENTITY WHERE (((TENANT_ID = ?) AND (ID = ?)) AND (TENANT_ID = ?))
bind => [1, 6, 1]
entityLine-2-1
entityLine-2-1
entityLine-2-1
entityLine-2-1
entityLine-2-1
entityLine-2-1
entityLine-2-1
entityLine-2-1
entityLine-2-1
entityLine-2-2
entityLine-2-2
entityLine-2-2
entityLine-2-2
entityLine-2-2
entityLine-2-2
entityLine-2-2
entityLine-2-2
entityLine-2-2
entityLine-2-3
entityLine-2-3
entityLine-2-3
entityLine-2-3
entityLine-2-3
entityLine-2-3
entityLine-2-3
entityLine-2-3
entityLine-2-3
entity-3
entityLine-3-1
entityLine-3-1
entityLine-3-1
entityLine-3-1
entityLine-3-1
entityLine-3-1
entityLine-3-1
entityLine-3-1
entityLine-3-1
entityLine-3-2
entityLine-3-2
entityLine-3-2
entityLine-3-2
entityLine-3-2
entityLine-3-2
entityLine-3-2
entityLine-3-2
entityLine-3-2
entityLine-3-3
entityLine-3-3
entityLine-3-3
entityLine-3-3
entityLine-3-3
entityLine-3-3
entityLine-3-3
entityLine-3-3
entityLine-3-3
entity-4
entityLine-4-1
entityLine-4-1
entityLine-4-1
entityLine-4-1
entityLine-4-1
entityLine-4-1
entityLine-4-1
entityLine-4-1
entityLine-4-1
entityLine-4-2
entityLine-4-2
entityLine-4-2
entityLine-4-2
entityLine-4-2
entityLine-4-2
entityLine-4-2
entityLine-4-2
entityLine-4-2
entityLine-4-3
entityLine-4-3
entityLine-4-3
entityLine-4-3
entityLine-4-3
entityLine-4-3
entityLine-4-3
entityLine-4-3
entityLine-4-3
Why TESTMULTITENANTENTITYLINE joins to itself t0/t1? This produce many duplicated records
I try to fetch record 2,3,4, why it fetch out 1, 5, 6?
Could you please help? is this a bug? Or I missed something? Thanks in advance.

It works after I removed tenant_id from the primary key by the following setting:
#TenantDiscriminatorColumn(primaryKey = false, ...)
But still, JOIN and EXIST option does not work although I do not need them.
It looks that in JOIN and EXIST sql the needed pids are not included in the where condition. and that's why it loads all other unneeded entries

Related

Maxima solve function find no solution

I am new to Maxima.
If I do:
solve([x = 2, y = 3], [x, y]);
I get:
[[x = 2, y = 3]]
Which is correct !
If I do:
solve([sin(x) = 1], [x]);
I get:
%pi
[x = ---]
2
Which is also correct !
But if I do:
solve([sin(x) = 1, y = 3], [x, y]);
I get:
[]
Why ?
Thanks for your help.
After load(to_poly_solve); try
to_poly_solve([sin(x)= 1, y = 3], [x,y]);

follower_type and followable_type in acts_as_follower gem giving different output in rails console and vs-code terminal

I'm using
gem 'acts_as_follower', github: 'tcocca/acts_as_follower', branch:
'master'
and I have 2 users, user1 is following user2. so, to check wheather user1 is following user2 I'm doing this
u1 = User.find(1) u2 = User.find(2) u1.following?(u2) # should return true
when I run this in rails console it's giving
Follow Count (4.1ms) SELECT COUNT(*) FROM "follows" WHERE "follows"."blocked" = ? AND "follows"."follower_id" = ? AND "follows"."follower_type" = ? AND "follows"."followable_id" = ? AND "follows"."followable_type" = ? [["blocked", 0], ["follower_id", 1], ["follower_type", "User"], ["followable_id", 2], ["followable_type", "User"]]
and in vs-code console it's giving
CACHE Follow Count (0.0ms) SELECT COUNT(*) FROM "follows" WHERE "follows"."blocked" = ? AND "follows"."follower_id" = ? AND "follows"."follower_type" = ? AND "follows"."followable_id" = ? AND "follows"."followable_type" = ? [["blocked", 0], ["follower_id", 1], ["follower_type", "ApplicationRecord"], ["followable_id", 2], ["followable_type", "ApplicationRecord"]]
any solution ?

Iterating of elements in array

I am trying to write a code of CPLEX OPL on an example of (from control systems) a typical MPC (Model Predictive Control) problem. As described, here:
With optimization variables:
With following parameters:
I have tried to write it but I am stuck at iteration of the array of variable "x" (state variable) as mentioned in the constraint of the optimization problem. The code I have so far written on OPL CPLEX is given as: (The model file as .mod extension on OPL platform)
//data
{string} state = ...;
{string} input = ...;
float A[state][state] =...;
float B[state][input] =...;
float Q[state] =...;
float R[input] =...;
//variable
dvar float State[state];
dvar float Input[input];
minimize
sum( s in state, u in input )
(State[s]*Q[s]*State[s] + Input[u]*R[u]*Input[u]);
subject to {
forall( s in state, u in input )
ct1:
A[s][s]*State[s] + B[s][u]*Input[u] == State[s+1];
}
And the data file which I am using is given as: (the data file of OPL platform with .dat extension)
state = {"x","y","vx","vy"};
input = {"ux","uy"};
A = [[1, 0, 0.2, 0],
[0, 1, 0, 0.2],
[0, 0, 1, 0 ],
[0, 0, 0, 1 ]];
B = [[0, 0],
[0, 0],
[0.2, 0],
[0, 0.2]];
Q = [[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1]];
R = [[1, 1],
[1, 1]];
Therefore, I need help kindly to solve this system as I am unable to solve the matter of iteration in the variable of the state variable in the constraint of the given problem.
Your kind help will be highly appreciated as I am stuck on this one for several weeks.
You can turn ct1 into
forall( s in state, u in input:s !=last( state ))
ct1:
A[s][s]*State[s] + B[s][u]*Input[u] == State[next(state,s)];
.mod
//data
{string} state = ...;
{string} input = ...;
float A[state][state] =...;
float B[state][input] =...;
float Q[state] =...;
float R[input] =...;
//variable
dvar float State[state];
dvar float Input[input];
minimize
sum( s in state, u in input )
(State[s]*Q[s]*State[s] + Input[u]*R[u]*Input[u]);
subject to {
forall( s in state, u in input:s !=last( state ))
ct1:
A[s][s]*State[s] + B[s][u]*Input[u] == State[next(state,s)];
}
.dat
state = {"x","y","vx","vy"};
input = {"ux","uy"};
A = [[1, 0, 0.2, 0],
[0, 1, 0, 0.2],
[0, 0, 1, 0 ],
[0, 0, 0, 1 ]];
B = [[0, 0],
[0, 0],
[0.2, 0],
[0, 0.2]];
Q = [1, 1, 1, 1]
;
R = [1, 1];
works

TensorFlow: varscope.reuse_variables()

How do I reuse variables in TensorFlow? I want to reuse the tf.contrib.layers.linear
with tf.variable_scope("root") as varscope:
inputs_1 = tf.constant(0.5, shape=[2, 3, 4])
inputs_2 = tf.constant(0.5, shape=[2, 3, 4])
outputs_1 = tf.contrib.layers.linear(inputs_1, 5)
varscope.reuse_variables()
outputs_2 = tf.contrib.layers.linear(inputs_2, 5)
But it gives me the following result
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-51-a40b9ec68e25> in <module>()
5 outputs_1 = tf.contrib.layers.linear(inputs_1, 5)
6 varscope.reuse_variables()
----> 7 outputs_2 = tf.contrib.layers.linear(inputs_2, 5)
...
ValueError: Variable root/fully_connected_1/weights does not exist, or was not created with tf.get_variable(). Did you mean to set reuse=None in VarScope?
The problem is tf.contrib.layers.linear automatically creates a new set of linear layers with its own scope. When calling scope.reuse() there's nothing to be reused because those are new variables.
Try to do something like this instead
def function():
with tf.variable_scope("root") as varscope:
inputs = tf.constant(0.5, shape=[2, 3, 4])
outputs = tf.contrib.layers.linear(inputs, 5)
return outputs
result_1 = function()
tf.get_variable_scope().reuse_variables()
result_2 = function()
sess = tf.InteractiveSession()
sess.run(tf.initialize_all_variables())
a = sess.run(result_1)
b = sess.run(result_2)
np.all(a == b) # ==> True
you just need to modify linear(inputs_1, 5) to linear(inputs_1, 5, scope="linear")
with tf.variable_scope("root") as varscope:
inputs_1 = tf.constant(0.5, shape=[2, 3, 4])
inputs_2 = tf.constant(0.5, shape=[2, 3, 4])
outputs_1 = tf.contrib.layers.linear(inputs_1, 5, scope="linear")
varscope.reuse_variables()
outputs_2 = tf.contrib.layers.linear(inputs_2, 5, scope="linear")

NHibernate Mapping By Code Cascade All-Delete-Orphans

How to set cascade to all-delete-orphans with mapping by code in NHibernate?
[Flags]
public enum Cascade
{
None = 0,
Persist = 2,
Refresh = 4,
Merge = 8,
Remove = 16,
Detach = 32,
ReAttach = 64,
DeleteOrphans = 128,
All = 256,
}
How can I combine All & DeleteOrphans?
Try to use:
r.Cascade(Cascade.All | Cascade.DeleteOrphans);
Because the Cascade is a [Flag] ... multi could be used:
What does the [Flags] Enum Attribute mean in C#?