I'm using Datanucleus with JPA config. Question is how datanucleus maps java Class<?> type? What is the representation in the database?
<datanucleus.version>3.1.3</datanucleus.version>
<datanucleus-enhancer.version>3.1.1</datanucleus-enhancer.version>
I can't find it in the documentation or on SO.
The answer lies in org.datanucleus.store.mapped.mapping.ClassMapping class, which uses org.datanucleus.store.types.converters.ClassStringConverter under the hood. ClassStringConverter uses
Class.forName(str) for member type conversion
klass.getName() for datastore type conversion
The Class<?> field only gets mapped when the #Basic annotation is used on the field.
Related
I can see the following description in Ballerina docs regarding type equivalence and inheritance.
Ballerina is based on type equivalence, rather than type inheritance. The type system in Ballerina is based on set theory and, therefore, type equivalence has more meaning for this domain than type inheritance.
So can somebody please let me know how this set theory can be used to implement the concept of 'inheritance' or the a similar functionality? How we can do re-use of functions/class variables, etc? Is there a standard way of doing so? And please share if there are examples/blogs I can refer to.
https://v0-991.ballerina.io/learn/faq/#why-is-there-no-type-inheritance
If I'm not mistaken, you are trying to map the OOP concepts found in popular languages such as Java to Ballerina right? While it's tempting to do so, that can actually be a counter productive effort. Instead of attempting to think of a solution to a problem in an object oriented manner and trying to write OOP-style code in Ballerina, it would be better to take time to get familiar with the type system and other constructs Ballerina provides and build up the solution using those constructs. The Ballerina by Examples (BBEs) would be a good place to start.
Having said that, I'll try to briefly answer the questions you've raised. The Ballerina type system is structural. In Java, any user defined type is an object and you use inheritance to establish the relationships between the types. In Ballerina, we compare the "shape" of the value to check if it is compatible with a particular type. Every value has a shape, and a type is a set of such shapes. Here's what the 2020R1 spec of the language says regarding this and subtyping:
A type denotes a set of shapes. Subtyping in Ballerina is semantic: a type S is a subtype of type T if the set of shapes denoted by S is a subset of the set of shapes denoted by T. Every value has a corresponding shape. A shape is specific to a basic type: if two values have different basic types, then they have different shapes.
Let's take a concrete example using records to further explain this.
type Person record {
string name;
int age;
};
type Student record {
string name;
int age;
string school;
};
public function main() {
Student st = {name: "John Doe", age: 18, school: "XYZ Academy"};
Person p = st; // this is a valid assignment
io:println(p);
}
In the above code snippet we can safely use a Person reference to manipulate a Student value since a Student value is guaranteed to have the same fields as a Person value.
The Student record definition can be written as follows as well:
type Student record {
*Person; // this is a type reference
string school;
};
Referring a type as given above copies all the fields in the specified record to the current record. While this may look like inheritance, it's not. The definition above is equivalent to the the original definition we saw earlier.
In Ballerina, code is organized by modules. Similar to packages in Java, except that a module is made up of functions, type definitions (e.g., records, objects), services, listeners, constants etc. While objects are supported, it's just another type of values; not a unit of organization for code. Functions are a module level construct and if you intend to reuse it in other modules, it needs to have the public access modifier. To call the function, you need to import the module and qualify the function call with the module name. e.g.,
int x = foo:barFunction();
Sharing variables across modules is not allowed in Ballerina. However, you can have public constants in a module. e.g.,
public const PI = 3.14;
Hope this clears things up. If you are interested in the design of the language, you can refer to the language spec I mentioned earlier and to the following blog posts from James:
Ballerina Programming Language - Part 0: Context
Ballerina Programming Language - Part 1: Concept
Also, note that 0.991 is a heavily outdated version. I'd recommend taking a look at the current version (1.2.2).
Ballerina does not support the implementation inheritance or class-based inheritance that you see in OO languages like Java. What this means is that you cannot inherit code from types in Ballerina (e.g. Ballerina objects).
The term inheritance is an overloaded term. If you want to know more about sybtyping in Ballerina, then read Pubudu's answer. It explains how you can achieve interface inheritance in Ballerina. You can map his answer to Ballerina objects as well.
How to create a Java class with a private final field in Clojure?
ClojureDocs for gen-class say that state field will be public
:state name
If supplied, a public final instance field with the given name will be
created.
So, in other words, do we have a way to create a class and after this a java object with the encapsulated state?
#alexmiller answered on this question recently here
In short, no. As you mention in the docs, gen-class state fields will
be public final fields. However, that field can be (for example), an
atom that is statefully modified by the implementation methods. In
general, we do not give much weight to encapsulation in Clojure -
instead preferring to make things visible, but "safe" (via
immutability). To quote Rich from
https://clojure.org/reference/datatypes, "encapsulation is folly".
gen-class is not a general purpose DSL for generating all possible
Java classes. It is a tool to generate classes in a certain style in
line with Clojure's aesthetics.
Another path however to something along these lines is to use a
deftype, which can have private mutable fields, exposed by
implementing interfaces or protocols inline. The deftype fields can
have meta of either ^:volatile-mutable or ^:unsynchronized-mutable,
both of which will become private fields.
What do .to_representation() and .to_internal_value do in serializers?
If I pass data to a serializer, is the data thrown to_representation() first?
What's the usage of these two?
If you want to create a custom field, you'll need to subclass Field
and then override either one or both of the .to_representation() and
.to_internal_value() methods. These two methods are used to convert
between the initial datatype, and a primitive, serializable datatype.
Primitive datatypes will typically be any of a number, string,
boolean, date/time/datetime or None. They may also be any list or
dictionary like object that only contains other primitive objects.
Other types might be supported, depending on the renderer that you are
using.
The .to_representation() method is called to convert the initial
datatype into a primitive, serializable datatype.
The to_internal_value() method is called to restore a primitive
datatype into its internal python representation. This method should
raise a serializers.ValidationError if the data is invalid.
Note that the WritableField class that was present in version 2.x no
longer exists. You should subclass Field and override
to_internal_value() if the field supports data input.
Ref:
http://www.django-rest-framework.org/api-guide/fields/#custom-fields
https://github.com/tomchristie/django-rest-framework/blob/master/rest_framework/serializers.py#L417
What returntype should I use when returning a Java object created with createObject("java", "<someclass>") from a function?
Is "Any" the only solution?
Yes, for java objects use type="any". Aside from "any", cffunction only supports basic types (string, numeric, struct, query, etcetera...). Everything else is assumed to be a component name. So using a java class name, such as java.lang.String, would cause an error because CF looks for a component with that path and obviously does not find it.
Is it possible to invoke in BPEL a webservice that has a return type an abstract class and at runtime returns any of the derived types?
E.g. if the return type is an order status which has a status field, and its subclasses having specific fields for different cases (valid order, invalid order, etc.).
The problem is that at invocation you have to specify an output variable that should be of this abstract type and subtype specific data couldn't be stored in a single type.
So far I have thought only of defining a data type that should accommodate all possible cases by having defined all fields of all derived classes.
Is there a better approach to this problem?
This should be possible, but may depend on the BPEL engine you are using.
I can recall that I have done similar processes in Apache ODE and WSO2 BPS.
If your BPEL engine does not support this, may be you can create several variables with the absolute types and use them appropriately in the invocations.
HTH