How can I insert Null value in the entity (EJB3)? - ejb

I'm working on EJB3. I want to insert Null value in the table.
Eg. I'm having a table called "Students" which has columns fields such as
String name;
String class;
Set<Subjects> subjects;
subjects field have the JoinTable "student_subjects" with fields joinColumns "student_name" of "Students" table and inverseJoinColumns "subjects_name" of "subjects" table
What I want is when I add a new student there will be a entry in "Students" table.
Also in the "student_subjects" table. but values must be student_name (not null) and NULL for "subjects_name". Is it possible?

You need to use nullable attribute of the #Column JPA annotation.
#Column(nullable = false)
private String studentName;
#Column(nullable = true)
private String subjectsName;

Related

Getting the current record of the lookup table

I need to get the value from a lookup field and need to auto-write this value to another string field. I used Modified field event to auto-populate string fields but when it comes to lookup I cannot get the value. I am dealing with this for 3 days. I would be really grateful if anyone can help...
I used this code to populate sting fields. And there needs to be field 3 which is lookup.
[
FormDataFieldEventHandler(formDataFieldStr(InventSite, InventSite, Field1), FormDataFieldEventType::Modified),
FormDataFieldEventHandler(formDataFieldStr(InventSite, InventSite, Field2), FormDataFieldEventType::Modified)
]
public static void Field1_OnModified(FormDataObject sender, FormDataFieldEventArgs e)
{
// get the form DataSource
FormDataSource dataSource = sender.datasource();
// get current record
InventSite inventSite = dataSource.cursor();
// contatenate string values
str details = strFmt("%1, %2", inventSite.Field1, inventSite.Field2);

How to id column value from table view from an other class to replace the count value?

public class Customer {
private static int count = 1;
public Customer(String x, String y) {
super();
this.x = new Simple-String-Property(x);
this.y = new Simple-String-Property(y);
this.id = new Simple-Integer-Property(count);
count++;
}
I have an abstract table which populate data from database after the application start. So i want my id column auto increment after retrieving data. So i use the variable count for static and it work fine. However, when i submit the abstract data to table, the id column back to 1 even though the data on the table show up different number depending from populated database.
First of all, are those dashes in the class name?
Second, if the ID is the primary key it might actually be a feature that you can't change it.
Change primary key (id) of a row in a table and shift the others downwards

How to rename a String variable dynamically using javassist?

I have a class named Sample and I need to rename the variable messageID to NameID, such that the corresponding getter and setter are also updated.
public class Sample{
String messageID;
public String getMessageID() {
return MessageID;
}
public void setMessageID(String messageID) {
MessageID = messageID;
}
}
With Javassist you can change the field name and all the references from old field name to the new one.
ClassPool classpool = ClassPool.getDefault();
CtClass ctClass = classpool.get(Sample.class.getName());
CtField field = ctClass.getField("messageID");
CodeConverter codeConverter = new CodeConverter();
codeConverter.redirectFieldAccess(field, ctClass, "NameID");
ctClass.instrument(codeConverter);
field.setName("NameID");
If you don't know ho to use Javassist you should read this tutorial here
The trick about "rewiring" all field references is done using a CodeConverter that will replace all references to the CtField field for the references to the field named NameID in ctClass. Keep in mind that this needs to be done before renaming the field into NameID.
However you should remember that all the references are updated but the set/get methods names are still getMessageID and setMessageID. You can easily change that using the same reference of ctClass as follow:
CtMethod getter = ctClass.getDeclaredMethod("getMessageID");
getter.setName("getNameId");
CtMethod setter = ctClass.getDeclaredMethod("setMessageID");
setter.setName("setNameId");

Azure table 'Merge' does not replace fields for which value passed is null (ex:Nullable<Date> field) C#

Azure Table 'Merge' does not merge the value of date if the date value for that record exists in the table and if we explicitly pass null to that Nullable field on merging.
I cannot use 'Replace' because the TableEntity defined in code is just a subset of the actual table with core fields defined and initialized. Replace will remove other dynamic fields added to it.
What you're observing is expected behavior of Merge Entity operation. From the documentation:
Any properties with null values are ignored by the Merge Entity
operation. All other properties will be updated.
A property cannot be removed with a Merge Entity operation. To remove
a property from an entity, replace the entity by calling the Update
Entity operation.
Apart from String data type where you can set the value as null, other data types do not support null. Setting a value of null on those attributes would simply mean you do not want to store them in the entity.
I guess the only option available to you is Replace (Update) the entity but based on the information you provided, it looks like you can't really use that option.
This is the solution.so far
Entity is the current updated record which needs to be replaced in the table.(Entity record has subset of the fields in the table for which null values are passed).
Any optimization or suggestions are welcome.
Code Snippet
TableQuery<DynamicTableEntity> rangeQuery = new TableQuery<DynamicTableEntity>().Where(
TableQuery.CombineFilters(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, Entity.PartitionKey),
TableOperators.And, TableQuery.GenerateFilterCondition("RowKey", QueryComparisons.Equal, Entity.RowKey)));
DynamicTableEntity CurrTblRec = table.ExecuteQuery(rangeQuery).FirstOrDefault();
if (CurrTblRec != null)
{
OperationContext OpContext = new OperationContext();
DynamicTableEntity EntityDyn = new DynamicTableEntity();
IDictionary<string, EntityProperty> EntityKV = TableEntity.WriteUserObject(Entity, OpContext);
Dictionary<string, EntityProperty> ExtraFldLt = CurrTblRec.Properties.Where(x => !EntityKV.ContainsKey(x.Key)).ToDictionary(x => x.Key, x => x.Value);
EntityDyn.ReadEntity(EntityKV, OpContext);
foreach (var AdditionalField in ExtraFldLt)
EntityDyn.Properties.Add(AdditionalField);
EntityDyn.PartitionKey = Entity.PartitionKey;
EntityDyn.RowKey = Entity.RowKey;
EntityDyn.ETag = "*";
TableOperation ReplaceEntity = TableOperation.Replace(EntityDyn);
tblRes = table.Execute(ReplaceEntity);
}
else
{
TableOperation insertOperation = TableOperation.Insert(Entity);
table.Execute(insertOperation);
}

set auto_incremented primary key explicitly while inserting the record in JDO

Enity primary key is an auto_incrimented ID in JDO
#Persistent(primaryKey = "true", valueStrategy = IdGeneratorStrategy.IDENTITY)
#Column(name = "ID")
private Long id;
When I am inserting any record, it's primary key ID is being set implicitly as auto_incremented.
I have a requirement in which I want set this ID (PK) using setter(setId()) explicitly while inserting the record.
Can we do this?
No. As comments say, you either use IDENTITY strategy (so its set by the datastore), or you set it yourself. You can't have both

Resources