Nick Shrine

Greenhorn
+ Follow
since Jul 17, 2003
Nick likes ...
Hibernate Netbeans IDE Tomcat Server
Merit badge: grant badges
For More
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 Nick Shrine

Yes that is the way to do it, need to add project to supervisor's collection of projects then em.merge(supervisor)
I have two entity classes with a bidirectional relationship thus:

class Supervisor {
@OneToMany
private Collection<Project> projects;
}

class Project {
@ManyToOne
private Supervisor supervisor;
}

Here is my test case:

public testCreateProject() throws Exception {
EntityManagerFactory emf = Persistence.createEntityManager("PU");

/* Retrieve the current list of projects */
EntityManager em = emf.createEntityManager();
Supervisor s = (Supervisor) em.find(Supervisor.class, 1);
int beforeSize = s.getProjects().size();
em.close();

/* Create a new project and save it */
Project p = new Project();
p.setSupervisor(s);
em = emf.createEntityManager();
em.getTransactio().begin();
em.persist(p);
em.getTransaction().commit();
em.close();

/* Retrieve updated list of projects */
em = emf.createEntityManager();
s = (Supervisor) em.find(Supervisor.class, 1);
int afterSize = s.getProjects().size();
em.close();

/* After adding a new project there should be +1 */
assertEquals(beforeSize + 1, afterSize);
}

The test always fails as beforeSize always equals afterSize. It is not re-reading the projects collection from the database.

Only after running the test again does the projects collection size increase by 1 indicating that there is some caching of the s object somewhere?

Any clues?
I took my exam on September 21st and today, some 10 weeks later (can anyone beat that?) got my result:

General Con: 100 93
Documentation: 70 65
OOD: 30 30
GUI: 40 33
Locking: 80 80
Data Store: 40 40
Network Server: 40 30
Total: 400 371

This forum was an invaluable resource of information for all those niggling worries, for example I decided to implement the 48 hr rule thing (I did URLyBird 1.1.3 BTW) after reading the woes of people who didn't on here.

My locking was very simple, I just had a Map of locked records in my Data class with the key being the record number and the value the lock cookie and all methods in my Data class were synchronized and that's about it!

The certification took me 2 years + 10 weeks waiting for my result (grrrr); Prometric said there was some database error that meant my exam and submission wasn't sent to the examiner until October 31st.

Anyway, thanks everyone, this forum is great! and now I'll try and post some responses instead of just questions!
18 years ago
I suppose because you can declare variables of that type e.g. if you have an interface called "myinterface" you can declare a variable:
myinterface myvar;
I've changed my mind, I think it's 4.
If the overloaded "=" operator creates objects on the heap then there is no point in having a String constant pool to save memory, so I reckon s1 and s2 point to objects in the constant pool, so there's 3 in the pool and 1 on the heap (the one made by the addition) - err or does that one go in the pool too?
Anyway I say 4
Nick - realised he doesn't understand how Strings work
I'd say 6 (but I'm not sure), here's why:
The objects "java", "exam" and " " are created in the string constant pool, the objects pointed to by the references s1 and s2 are created on the heap and the String object created by the addition (s1 + " " + s2) is created on the heap so you have 3 objects in the string constant pool and 3 on the heap, but I'm just guessing as I'm not sure what really goes on when you create a String with the overloaded "=" operator. Perhaps s1 and s2 point to objects in the constant pool rather than the heap? Can anyone clarify?
Nick

Originally posted by majohnad majohnad:
Hi,
void method X() {
String r = new String("abc");
String s = new String("abc");
r = r+1; //1
r = null; //2
s = s + r; //3
} //4

In the above code in which line s is available for garbage collection.
Thnaks in Advance


Unless I'm mistaken, I'd say line 3.
s initially contains a reference to the String object created by String s = new String("abc").
I always draw a diagram like this:
r ------------> (1)
s ------------> (2)
incrementing the number on the right (the heap) every time a new object is created and showing which reference variables point to which objects, then it is easy to see when an object is eligible for GC when it has no refs pointing to it. So in this case after line 3 we have:
(1)
(2)
(3)
r ------------> null
s ------------> (4)
So we can see that 3 objects are eligible for GC. (3) is the object created at line 1 and (4) is the new String object resulting from the addition at line 3.
Also, I presume your question should read:
"in which line is the object initially assigned to s eligible for GC". s itself is never eligible for GC as it is a stack variable and therefore not on the garbage collectible heap.
(I assume I am right in thinking that when you add Strings the new String does not contain references to the Strings that were added but merely constructs a new String object out of the data contained in the two added Strings.)

Originally posted by Bert Bates:
Nick -
Fantastic score!
It's great to hear that the mocks were accurate, so did you miss questions in the same areas as on the real exam?
-Bert


Threads gets me everytime and overriding/oveloading and constructors but I know a couple of ones I got wrong that were silly mistakes (kick kick).
20 years ago
I used Kathy and Bert's book which pretty much covers all you need to know.
For mock exams I used the MasterExams that come with the book, Marcus Green's 1.4 exams (http://www.examulator.com/jezam/login.jsp) and Dan's exams (http://www.danchisholm.net/index.html).
I'd say Marcus' exams are at the right level, Dan's are too clever - I think he swallowed a copy of the JLS! - good practice nevertheless (and a bit of a challenge!), but watch you don't get side-tracked worrying about the really obscure stuff in there - as long as you know what Kathy & Bert's book says you need to know.
Despite the MasterExam software being annoying the material must be spot-on because I got 55/61 in both MasterExams then 55/61 in the real thing!
Phew! I never want to see one of those cheeky questions again! Now I can get back to my coding.
Nick
20 years ago
why does:
int i = 100;
byte b = i;
not compile, wheras:
final int i = 100;
byte b = i;
does?
20 years ago