Query for oneToMany relationship - sql

I am looking for solve my issue.
I have something like this.
class A{
#OneToMany(mappedBy="a")
private List<B> bs;
}
class B {
#ManyToOne
private A a;
private String name;
#Lob
private byte[] img;
}
I want get all records from database except img field from relationship B.
Maybe create query in JPA but how do this correct?
How can I do that?

You could define img as lazy:
#Basic(fetch=FetchType.LAZY)
#Lob
private byte[] img;
That way the image is only loaded when you access the getter.
Second option is to create a DTO (Data transfer object) using the constructor expression:
package dto;
public class BDto {
private final String name;
private final A a;
public BDto(String name, A a) {
this.name = name;
this.a = a;
}
// getters for both fields
}
Then you can create a query like:
select new dto.BDto(b.name, b.a) from B b where b.a.received = true
Important notice: the dto must be used in the query fully qualified (e.g. dto.BDto)

Related

Jackson fails with "Cannot construct instance of WorkpoolId (although at least one Creator exists): no int/Int-argument constructor/factory"

I have the following class
public class WorkpoolId implements Serializable {
#NotNull
private Long id = null;
#JsonCreator
public WorkpoolId(#JsonProperty("workpoolId") long id) {
this.id = Long.valueOf(id);
}
public WorkpoolId(Long id) {
this.id = id;
}
public WorkpoolId(String id) {
this.id = Long.valueOf(id);
}
private WorkpoolId() {
}
}
when mapping
"workpoolId":1
to this class I get a
javax.ws.rs.ProcessingException: com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of WorkpoolId (although at least one Creator exists): no int/Int-argument constructor/factory method to deserialize from Number value (1)
Why is jackson not able to use the long constructor for the number value?
It fails because your WorkpoolId does not have access to field workpoolId it is not in its context anuymore. When your JSON is deserialized it could be deserialized either as an
independent object (having no field workpoolId, it IS the workbookId)
field object value in an object containing -say Data - it that might be named as workpoolId.
Now the use of workbookId could be usable for the JsonCreator in Data when constructing its field workpoolId.
To clarify this a bit, here is an example of possible Data class:
#Getter #Setter
public class Data {
private WorkpoolId workpoolId;
#JsonCreator // here it is a property!
public Data(#JsonProperty("workpoolId") long id) {
this.workpoolId = new WorkpoolId(id);
}
}
Json would be like {"workpoolId":1}
To have it work just remove the annotation #JsonProperty("workpoolId") from the attribute declaration. Actually the whole #JsonCreator annotation is not needed.

Can I refer to properties directly in entity constructor with EclipseLink?

EclipseLink version is 2.5.1
We've moved from GlassFish web-server to TomCat. This made us switch to static weaving because with TomCat dynamic weaving doesn't really work that easy.
Now that static weaving works, it seems to work quite a bit differently.
If I have an entity which sets some property directly in the constructor:
class Entity {
#Column
private String name;
public Entity() {
name = "something";
}
public String getName() {
return name;
}
}
Long story short this test will fail:
Entity e = new Entity();
assertEquals("something", e.getName()); // e.getName() returns null
This happens because getName(), after weaving, is not returning this.name anymore. Instead it calls a routing for initialization (if it's needed) and (I guess) gets the value of the property from some underlying HashMap.
But constructor is not being weaved, I even have looked into the sources of weaver and seems to be explicitly opting out of this:
/**
* Construct a MethodWeaver and allow it to process the method.
*/
#Override
public MethodVisitor visitMethod(int access, String methodName, String desc, String signature, String[] exceptions) {
MethodVisitor mv = super.visitMethod(access, methodName, desc, signature, exceptions);
if (!alreadyWeaved) {
// skip constructors, they will not changed
if (!"<init>".equals(methodName) && !"<cinit>".equals(methodName)) {
// remaining modifications to the 'body' of the class are
// delegated to MethodWeaver
mv = new MethodWeaver(this, methodName, desc, mv);
}
}
return mv;
}
The question is, maybe I miss something here? Is it the actual reality with EclipseLink 2.5.1 that you can't use properties directly in entity's own ctor? (and it's not even mentioned anywhere, not googlable at least)
It turns out yes, we can.
But there was a problem that led us to the property being not visible to the getter.
We actually have MappedSuperclass inheritance here and we were shadowing this field in the child class. Essentially this:
class A {
#Column()
protected String name;
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
class B extends A {
#Column()
protected String name;
// no #Override here
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
So we were just shadowing the property.

Arraylist what am i actually doing here

Suppose am in a class
`public class Foo{
//some fields
ArrayList fooList = new ArrayList<>();
//methods
Public void addFoo(){
//some code
fooList.add(this);
}
}`
wrote a method call addFoo() in 'foo' class. So now my question is what am I doing when I say arraylistObject.add(this) inside addFoo()... What am I actually doing.
Please find the below usecase which might help you.
A data structure that stores a collection of the same structure will create a tree implicitly.
public class TreeSample {
private String value;
private List<TreeSample> childNodes;
TreeSample(String value) {
this.value = value;
childNodes = new LinkedList<TreeSample>();
}
public void addChild(TreeSample childTree) {
this.childNodes.add(childTree);
}
}
So, the client code can construct a tree data structure, by adding child trees to the parent nodes.

Dropwizard hiding field in json

What's the best way to hide a field on a model in Dropwizard? (If I want to be able to deserialize the field via Jackson, but hide it when serialization)
For example, if I have the following model:
class User {
private String secret;
private String username;
}
I want to be able to create a User with a secret via calling new ObjectMapper().readValue(), but I want to hide the secret field it when it's serialized into JSON.
Add #JsonIgnore annotation before the property
Or you can add #JsonIgnoreProperties annotation and specify the fields which are to be excluded
public class Foo{
#JsonIgnore
private String bar;
...
}
or
#JsonIgnoreProperties(value = { "bar" })
public class Foo {
private String bar;
...
}
or if you want to ignore this field only on serialization, and not on deserialization then
public class Foo{
private String bar;
...
#JsonIgnore
public String getBar(){
return bar;
}
public void setBar(String bar){
this.bar = bar;
}
}
If you want to hide it only during seriazliation add #JsonIgnore annotation to property getter.
class User {
#JsonProperty
private String secret;
#JsonProperty
private String username;
#JsonIgnore
public String getSecret(){
return secret;
}
public void setSecret(String secret){
this.secret = secret;
}
...
}

How to persist an enum using NHibernate

Is there a way to persist an enum to the DB using NHibernate? That is have a table of both the code and the name of each value in the enum.
I want to keep the enum without an entity, but still have a foreign key (the int representation of the enum) from all other referencing entities to the enum's table.
Why are you guys over complicating this? It is really simple.
The mapping looks like this:
<property name="OrganizationType"></property>
The model property looks like this:
public virtual OrganizationTypes OrganizationType { get; set; }
The Enum looks like this:
public enum OrganizationTypes
{
NonProfit = 1,
ForProfit = 2
}
NHibernate will automatically figure it all out. Why type more than you need????
You can use the enum type directly: http://web.archive.org/web/20100225131716/http://graysmatter.codivation.com/post/Justice-Grays-NHibernate-War-Stories-Dont-Use-Int-If-You-Mean-Enum.aspx. If your underlying type is a string, it should use the string representation, if it is numeric, it will just use the numeric representation.
But your question wording sounds like you're looking for something different, not quite an enum. It seems that you want a lookup table without creating a separate entity class. I don't think this can be done without creating a separate entity class though.
An easy but not so beautiful solution:
Create an integer field with and set the mapping in the mapping file to the field.
Create a public property that uses the integer field.
private int myField;
public virtual MyEnum MyProperty
{
get { return (MyEnum)myField; }
set { myField = value; }
}
I am using NHibernate 3.2, and this works great:
type="NHibernate.Type.EnumStringType`1[[enum_full_type_name, enum_assembly]], NHibernate"
Not sure when the generic EnumStringType got added, though.
Try using a stategy pattern. Uou can then put logic into your inner classes. I use this quite alot espically when there is logic that should be contained in the "enum". For example the code below has the abstract IsReadyForSubmission() which is then implemented in each of the nested subclasses (only one shown). HTH
[Serializable]
public abstract partial class TimesheetStatus : IHasIdentity<int>
{
public static readonly TimesheetStatus NotEntered = new NotEnteredTimesheetStatus();
public static readonly TimesheetStatus Draft = new DraftTimesheetStatus();
public static readonly TimesheetStatus Submitted = new SubmittedTimesheetStatus();
//etc
public abstract int Id { get; protected set; }
public abstract string Description { get; protected set; }
public abstract bool IsReadyForSubmission();
protected class NotEnteredTimesheetStatus: TimesheetStatus
{
private const string DESCRIPTION = "NotEntered";
private const int ID = 0;
public override int Id
{
get { return ID; }
protected set { if (value != ID)throw new InvalidOperationException("ID for NotEnteredTimesheetStatus must be " + ID); }
}
public override string Description
{
get { return DESCRIPTION; }
protected set { if (value != DESCRIPTION)throw new InvalidOperationException("The description for NotEnteredTimesheetStatus must be " + DESCRIPTION); }
}
public override bool IsReadyForSubmission()
{
return false;
}
}
//etc
}