Win a copy of Microservices Testing (Live Project) this week in the Spring forum!

Laura Barroso

Ranch Hand
+ Follow
since Sep 21, 2009
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Laura Barroso

I'm not an expert but I think that you should look at Category class and Money class:
# @OneToMany(mappedBy="category", cascade={CascadeType.ALL})
# public List<Money> getMoneys() {
#
# return moneys;
# }
//////////////////////////////////////////////////////////////////////////////
@Id
@ManyToOne(cascade={CascadeType.PERSIST, CascadeType.REFRESH})
@JoinColumn(name="categoryname")
public Category getCategory() {

return category;
}
When you said that a collection is mappedBy "category" JPA try to find it in the other end of the relationship, and when it find it the type is not the same... you have a List<Money> mapped by a Category object...I think that's the problem...
The problem is about if it is a good approach to put a class with a OneToMany relationship, even if it doesn't need to know about the collection that it holds in order to let the container handle everything with cascade...
That's what I think too: I mean, I could fix the problem with multiplicity OneToMany, but...they said to me that this approach make my class dirty because every time that I have to bring the entity that holds the OneToMany relationship then it will load the entire collection, but I think that if I put fetch = fecth.LAZY I won't need to worry about that collection….
No, I don't have a join table...I just have this relationship(ManyToOne)
I know that an unidirectional ManyToOne doesn’t support deletion in cascade so my solution was to put it bidirectional…of course, this bring the issue that you probably don’t need it in the other end of the relationship…but I think that I could place the getter and setter methods that hold my relation private and fetch my collection lazy…there! Problem solves!

However when I consult my boss he says that this was not a good approach to solve this problem, that my solution make the classes dirty!, that he rather to make this deletion thing with a query and then delete the other entity (the entity who should have the OneToMany side).

So my question is: is this true? My solution isn’t a good approach???


Hi, I have a OneToMany bidirectional relationship, from Workspace to Map, and Map to Layers…the relation seems to work just fine between Workspace and Map, however when I try to load the Layers that are contains in Map it only load one Layer… what could be the problem???
This is my code:

@Entity
@Table(name = "workspaces" ,
uniqueConstraints = {@UniqueConstraint(columnNames = {"name"} ) })
@SequenceGenerator(name = "workspace_sequence", sequenceName = "workspace_id_seq")
public class Workspace extends EntityBean<Integer> implements IWorkspace {
private Set<Map> maps = new HashSet<Map>();
@OneToMany( mappedBy="mapWorkspace", cascade={ CascadeType.ALL},
fetch=FetchType.EAGER)
@Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
public Set<Map> getWorkspaceMaps() {
return maps;
}

public void setWorkspaceMaps(Set<Map> maps) {
this.maps = maps;
}
….
}

@Entity
@Table(name = "maps")
@SequenceGenerator(name = "map_sequence", sequenceName = "map_id_seq")
public class Map extends EntityBean<Integer> implements IMap {
private SortedSet<MapLayer> layerList = new TreeSet<MapLayer>();
private Workspace workspace;

@ManyToOne
@JoinColumn(name = "workspace_id", referencedColumnName="workspace_id")
public Workspace getMapWorkspace() {
return this.workspace;
}

public void setMapWorkspace(Workspace workspace) {
this.workspace = workspace;
}

@OneToMany( mappedBy = "layerMap", cascade={CascadeType.ALL} ,
fetch = FetchType.EAGER )
@OptimisticLock(excluded = true)
@Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
@Sort(comparator = org.desoft.geo.entity.LayerPosComparator.class,
type = SortType.COMPARATOR)
public SortedSet<MapLayer> getMapLayers() {
return layerList;
}

public void setMapLayers(SortedSet<MapLayer> layers) {
this.layerList = layers;
}
}

@Entity
@Table(name = "layers")
@SequenceGenerator(name = "layer_sequence", sequenceName = "layer_id_seq")
@Inheritance(strategy=InheritanceType.JOINED)
public class MapLayer extends MapElementBase<Integer> implements IMapLayer, Serializable {

@ManyToOne
@JoinColumn(name="map_id",referencedColumnName="map_id")
public Map getLayerMap() {
return (Map) this.map;
}

public void setLayerMap(Map map) {
this.map = map;
}
}
Well Mark, you were rigth...the problem was that I didn't put the name of the table rigth...however when I add another class that inherit from Animal I get this exception :
ERROR [AbstractKernelController] Error installing to Start: name=persistence.unit:unitName=#DataServicePU state=Create
org.hibernate.AssertionFailure: Table animal_state not found

And here is my code:
@Entity
@Table(name="animals")
@Inheritance(strategy=InheritanceType.JOINED)
@SecondaryTable(name = "animal_state",
pkJoinColumns={ @PrimaryKeyJoinColumn(name="animal_id")})
public class Animal extends EntityBean<Integer> {

protected String name;
protected Integer temperature;
public Animal()
{}

@Id
@Column(name="animal_id")
public int getAnimalId() {
return this.beanId.intValue();
}

public void setAnimalId(int id) {
this.beanId = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}



@Transient
public String getEntityKey() {
return new Integer(this.beanId).toString();
}

@Transient
public String getDisplayValue() {
return "Animal";
}


@Column(name="temperature", table = "animal_state",nullable=true)
public int getTemperature() {
return temperature;
}

public void setTemperature(int temperature) {
this.temperature = temperature;
}

}


@Entity
@Table(name="dogs")
@SecondaryTable(name="dog_states",
pkJoinColumns={@PrimaryKeyJoinColumn(name="animal_id")})
public class Dog extends Animal {

private String collarColor;
private String animo;
public Dog() {

}

public String getCollarColor() {
return collarColor;
}

public void setCollarColor(String collarColor) {
this.collarColor = collarColor;
}

/*@Override
@OneToOne
@PrimaryKeyJoinColumn(name="animal_id",referencedColumnName="animal_id")
public DogState getActualState() {
return (DogState) super.getActualState();
}*/


@Column(name = "animo", table="dog_states",nullable=true)
public String getAnimo() {
return animo;
}

public void setAnimo(String animo) {
this.animo = animo;
}

}



Hi, I'm trying to use the @SecondaryTable in my app, however, when I try to deploy it this exception arise:
rg.hibernate.AnnotationException: Cannot find the expected secondary table: no animal_state available for test.Animal
at org.hibernate.cfg.Ejb3Column.getJoin(Ejb3Column.java:293)
at org.hibernate.cfg.Ejb3Column.getTable(Ejb3Column.java:272)

at org.hibernate.cfg.annotations.SimpleValueBinder.make(SimpleValueBinder.java:222)
at org.hibernate.cfg.annotations.PropertyBinder.bind(PropertyBinder.java:122)
at org.hibernate.cfg.AnnotationBinder.processElementAnnotations(AnnotationBinder.java:1667)
at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:754)

Here is my code:
@Entity
@Table(name="animals")
@Inheritance(strategy=InheritanceType.JOINED)
@SecondaryTable(name = "animal_states",
pkJoinColumns={
@PrimaryKeyJoinColumn(name="animal_id")})
public class Animal extends EntityBean<Integer> implements Serializable {

protected String name;


@Id
@Column(name="animal_id")
public int getAnimalId() {
return this.beanId.intValue();
}

public void setAnimalId(int id) {
this.beanId = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}



@Transient
public String getEntityKey() {
return new Integer(this.beanId).toString();
}

@Transient
public String getDisplayValue() {
return "Animal";
}

protected int temperature;
@Column(name="temperature", table = "animal_state")
public int getTemperature() {
return temperature;
}

public void setTemperature(int temperature) {
this.temperature = temperature;
}

}

Hi, I have a problem with the function NativeQuery of the entityManager, here is my code:

public List executeNativeQuery(String aQuery, Class itemClass,int max) {

Query query = entityManager.createNativeQuery(aQuery, itemClass);
if (max > 0)
query.setMaxResults(max);
return query.getResultList();
}

But when I call this function I got this exception:

java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) .....
Caused by: java.lang.NullPointerException
at org.jboss.ejb3.stateless.StatelessContainer.dynamicInvoke(StatelessContainer.java:379)
at org.jboss.ejb3.session.InvokableContextClassProxyHack._dynamicInvoke(InvokableContextClassProxyHack.java:53)
at org.jboss.aop.Dispatcher.invoke(Dispatcher.java:91)
at org.jboss.aspects.remoting.AOPRemotingInvocationHandler.invoke(AOPRemotingInvocationHandler.java:82)
at org.jboss.remoting.ServerInvoker.invoke(ServerInvoker.java:891)
Hi I'm facing this problem: I have a OneToMany relationship, here is my code:
@OneToMany(mappedBy = "school")
@Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
public List<Student> getStudents() {
return students;
}

public void setStudents(List<Student> students) {
this.students = students;
}
But..when I try to deploy my app netbeans says:
warning: Cannot find annotation method 'value()' in type 'org.hibernate.annotations.Cascade': class file for org.hibernate.annotations.Cascade not found
An exception has occurred in the compiler (1.6.0). Please file a bug at the Java Developer Connection (http://java.sun.com/webapps/bugreport) after checking the Bug Parade for duplicates. Include your program and the following diagnostic in your report. Thank you.
com.sun.tools.javac.code.Symbol$CompletionFailure: class file for org.hibernate.annotations.CascadeType not found

I don't know if it is a library that is missing or something else...anyone knows what could be happen???
Hi, I'm having problems with this query:
Timestamp initialDate= Timestamp.valueOf("2010-11-11 10:00:00");
Timestamp finalDate= Timestamp.valueOf("2010-11-11 10:00:00");
String deviceId = "001";
Query query = entityManager.createQuery("from StoredMobileState s where " +
"lower(s.deviceId) = lower(:deviceId) and " +
"s.localTimestamp between "+initialDate+" and "+finalDate);
query.setParameter("deviceId", deviceId);
query.setParameter("initialDate", initialDate);
query.setParameter("finalDate", finalDate);

When I try it my server gives me this exception:
java.lang.IllegalArgumentException: org.hibernate.hql.ast.QuerySyntaxException: unexpected token: 10 near line 1, column 131 [from org.desoft.dcp.entity.StoredMobileState s where lower(s.deviceId) = lower(:deviceId) and s.localTimestamp between 2010-11-11 10:00:00.0 and 2010-11-11 10:01:00.0]

finalDate and initialDate are Timestamp objects...Does anyone knows how to fix this???
problem solve, thank you very much
Perfect! just one more question... what library should I include in my code in order to get this annotation work...because netBeans doesn't appear to recognize org.jboss.annotation.Service or org.jboss.annotation.ejb.Management...so what libraries should I include???
No, they don't...What I'm looking for is an annotation that gives me the oportunity for make a call to a business function that I have in my app, but I only need to make this call when I'm deploying my app, the problem now with @Startup is that is only available in ejb 3.1 and my app is in ejb 3.0, so I'm looking for alternatives...One friend of mine says that maybe I could resolve my problem with @Service, but I haven't found much information, does anyone have an example or doc about this annotation???