|
Thread Rules 1. This is not a "do my homework for me" thread. If you have specific questions, ask, but don't post an assignment or homework problem and expect an exact solution. 2. No recruiting for your cockamamie projects (you won't replace facebook with 3 dudes you found on the internet and $20) 3. If you can't articulate why a language is bad, don't start slinging shit about it. Just remember that nothing is worse than making CSS IE6 compatible. 4. Use [code] tags to format code blocks. |
Speaking about teaching each other stuff: Where do you put your constants in OOP? So far I came across 3 types of storing them:
1. In the class itself. 2. In the interface. 3. In a separate, final class whose only purpose is to store constants.
Is there one preferred way or is it all situational for you?
Edit:
Or perhaps not using constants and instead relying on final methods that return the value you want is better? Found some resources stating it's preferable due to better encapsulation and what not.
|
On November 23 2015 16:38 Manit0u wrote: Speaking about teaching each other stuff: Where do you put your constants in OOP? So far I came across 3 types of storing them:
1. In the class itself. 2. In the interface. 3. In a separate, final class whose only purpose is to store constants.
Is there one preferred way or is it all situational for you?
Edit:
Or perhaps not using constants and instead relying on final methods that return the value you want is better? Found some resources stating it's preferable due to better encapsulation and what not. Depends on the constant but for me it is 3-1-2 from the most frequent to the least.
|
On November 23 2015 16:38 Manit0u wrote: Speaking about teaching each other stuff: Where do you put your constants in OOP? So far I came across 3 types of storing them:
1. In the class itself. 2. In the interface. 3. In a separate, final class whose only purpose is to store constants.
Is there one preferred way or is it all situational for you?
Edit:
Or perhaps not using constants and instead relying on final methods that return the value you want is better? Found some resources stating it's preferable due to better encapsulation and what not.
The most common and in my opinion best way is a "SomeEnum" class that essentially just contains the constants. This has the advantage that the constant can be used in many places without necessarily also requiring the class that the constants belong to. As an example, in a client-server architecture both sides can use the enum class, even if the values are returned by a specific class for the server.
I always hesitate to assign constants in interfaces, because for me interfaces are purely descriptive while constants hold values and don't just describe those values, but I'm kind of against the stream with that opinion. If I need constants only across a class hierarchy and decide for an in-class constant then I use abstract classes instead of interfaces.
I only use in-class constants for constant values that don't have to be configured and are not used outside the class(hierarchy), i.e. essentially private or protected constants. It's a fairly rare case for me, but occasionally a private constant makes sense.
Final methods that return the value is, at least for me, undesirable since you bind yourself as tightly to the class as using constants in the first place. You potentially lose out on performance due to the method call, depending on whether the compiler/interpreter optimizes it away, and still have the same result with a bit more syntax tacked on. Unless you make the methods static you also need an instance of the class, which isn't always desirable or possible. On the other hand, using a method means that you can later switch out the constant against a variable, but then you made the mistake of using a constant for something that is not constant in the first place.
Depending on the use-case, all variants are valid and are used. In general I'd lean to using Enum classes for most cases and fall back on in-class abstracts when the constants are essentially private or protected and not required outside the immediate class hierarchy.
|
That depends on the usage of those constants. If they are only important inside the class itself then put them into the class. If the constant is used only a few classes that are tightly coupled then you may outsource them or put them in the biggest-most-important class of them all. If we are talking about constants that are supposed to be used as arguments, symbolic values that the user is expected to know, then put them in a separate class.
|
I was looking through my CAs and I have one from AVG Technologies - the company that makes AVG. They don't seem to be a CA authority, but they have a root CA on my computer. This doesn't seem like a good thing. Why does AVG need their own CA?
|
That's a trick by the programs that have a feature where they inspect what your browser receives. It needs the certificate to sabotage HTTPS/TLS encryption or it would only be able to do things to HTTP traffic. I think it decrypts stuff before it reaches the browser, then encrypts it with its own certificate. It needs to fake that everything is still authorized correctly, so needs its own fake root CA.
I'm sure I explained this a bit wrong and used wrong terms, misunderstood details about what's going on exactly, etc. I only looked a bit into this when I tried to set up a squid proxy where I wanted it to be able to cache files transmitted through HTTPS. It wasn't really possible to hack the security that's in the current browsers. They always caught that I was doing something to the data with the certificate I had created myself and that it didn't really come from the correct sender.
I'd worry about what programs like your AVG stuff are doing there to hack the browser's security. I think I'd rather not use this feature of an Antivirus suite.
|
On November 24 2015 02:48 Ropid wrote: That's a trick by the programs that have a feature where they inspect what your browser receives. It needs the certificate to sabotage HTTPS/TLS encryption or it would only be able to do things to HTTP traffic. I think it decrypts stuff before it reaches the browser, then encrypts it with its own certificate. It needs to fake that everything is still authorized correctly, so needs its own fake root CA.
I'm sure I explained this a bit wrong and used wrong terms, misunderstood details about what's going on exactly, etc. I only looked a bit into this when I tried to set up a squid proxy where I wanted it to be able to cache files transmitted through HTTPS. It wasn't really possible to hack the security that's in the current browsers. They always caught that I was doing something to the data with the certificate I had created myself and that it didn't really come from the correct sender.
I'd worry about what programs like your AVG stuff are doing there to hack the browser's security. I think I'd rather not use this feature of an Antivirus suite. Yeah. Our corporate https proxy does something similar and randomly breaks sites. It fucking sucks
|
How can I select MySQL rows from a certain starting id to an end id in one query?
Previously I was just selecting one row at a time but in hindsight I'm sure that must be extremely inefficient compared to just grabbing, say, 100 rows at a time.
|
Use the keyword WHERE with something like id > somevalue and id < somevalue?
|
EDIT: Solved
I have an issue where I have to do a batch insert/update in Java. I'm using spring boot and hibernate jpa.
Currently it takes 10s to do 1000 records using the simple CrudRepository. Under 1s to do 30 records. So it'll take 10 days to insert 100M records and then switch the databases. I can't wait that long. It might be possible to just update the changed records but we're using Oracle so I'm not sure if possible.
... @Service public class AttributeServiceImpl implements AttributeService { @Value("${hibernate.jdbc.batch_size}") private int BATCH_SIZE;
@Autowired private EntityManagerFactory emf;
...
@Override public void insertBatchCollectorAttributes(List<Attribute> attributes) { EntityManager entityManager = emf.createEntityManager(); EntityTransaction tx = entityManager.getTransaction(); tx.begin(); for (int i = 0; i < attributes.size(); i++) { Attribute attribute = attributes.get(i); entityManager.persist(attribute); if (i % BATCH_SIZE == 0) { entityManager.flush(); entityManager.clear(); } attributes.add(attribute); } entityManager.flush(); entityManager.clear(); entityManager.close(); } }
Every time it gets to entityManager.flush() the method hangs. Looking into the source code it hangs on this:
// Invoke method on current EntityManager. try { return method.invoke(this.target, args);
How do I get the code to stop hanging.
Thank you TL rockstars and thank you!
|
On November 24 2015 07:22 obesechicken13 wrote:I have an issue where I have to do a batch insert/update in Java. I'm using spring boot and hibernate jpa. Currently it takes 10s to do 1000 records using the simple CrudRepository. Under 1s to do 30 records. So it'll take 10 days to insert 100M records and then switch the databases. I can't wait that long. It might be possible to just update the changed records but we're using Oracle so I'm not sure if possible. ... @Service public class AttributeServiceImpl implements AttributeService { @Value("${hibernate.jdbc.batch_size}") private int BATCH_SIZE;
@Autowired private EntityManagerFactory emf;
...
@Override public void insertBatchCollectorAttributes(List<Attribute> attributes) { EntityManager entityManager = emf.createEntityManager(); EntityTransaction tx = entityManager.getTransaction(); tx.begin(); for (int i = 0; i < attributes.size(); i++) { Attribute attribute = attributes.get(i); entityManager.persist(attribute); if (i % BATCH_SIZE == 0) { entityManager.flush(); entityManager.clear(); } attributes.add(attribute); } entityManager.flush(); entityManager.clear(); entityManager.close(); } } Every time it gets to entityManager.flush() the method hangs. Looking into the source code it hangs on this: // Invoke method on current EntityManager. try { return method.invoke(this.target, args); How do I get the code to stop hanging. Thank you TL rockstars and thank you!  Just googling EntityTransaction results in some examples. The thing missing from your code is "tx.commit()". Maybe that has something to do with it?
|
On November 24 2015 07:22 obesechicken13 wrote: for (int i = 0; i < attributes.size(); i++) { Attribute attribute = attributes.get(i); ... attributes.add(attribute); } }
what are you trying to do here? also when you begin a Transaction you might want to commit it at some point in time, no?
|
On November 24 2015 07:46 Prillan wrote:Show nested quote +On November 24 2015 07:22 obesechicken13 wrote:I have an issue where I have to do a batch insert/update in Java. I'm using spring boot and hibernate jpa. Currently it takes 10s to do 1000 records using the simple CrudRepository. Under 1s to do 30 records. So it'll take 10 days to insert 100M records and then switch the databases. I can't wait that long. It might be possible to just update the changed records but we're using Oracle so I'm not sure if possible. ... @Service public class AttributeServiceImpl implements AttributeService { @Value("${hibernate.jdbc.batch_size}") private int BATCH_SIZE;
@Autowired private EntityManagerFactory emf;
...
@Override public void insertBatchCollectorAttributes(List<Attribute> attributes) { EntityManager entityManager = emf.createEntityManager(); EntityTransaction tx = entityManager.getTransaction(); tx.begin(); for (int i = 0; i < attributes.size(); i++) { Attribute attribute = attributes.get(i); entityManager.persist(attribute); if (i % BATCH_SIZE == 0) { entityManager.flush(); entityManager.clear(); } attributes.add(attribute); } entityManager.flush(); entityManager.clear(); entityManager.close(); } } Every time it gets to entityManager.flush() the method hangs. Looking into the source code it hangs on this: // Invoke method on current EntityManager. try { return method.invoke(this.target, args); How do I get the code to stop hanging. Thank you TL rockstars and thank you!  Just googling EntityTransaction results in some examples. The thing missing from your code is "tx.commit()". Maybe that has something to do with it? I thought flushing did an implicit commit lol. But I tried that earlier today too, my commit code is commented out lol.
I think crudrepository has an iterable save method so I'm just using that now instead. False alarm people!
Thanks!
|
On November 24 2015 07:00 Itsmedudeman wrote: Use the keyword WHERE with something like id > somevalue and id < somevalue?
You can use either of these statements:
SELECT * FROM `table` WHERE `id` >= 1 AND `id` <= 100
SELECT * FROM `table` WHERE `id` BETWEEN 1 AND 100
This code is pretty much equivalent. Personally I prefer to use BETWEEN as it's more readable (just remember it's inclusive). It also works nicely with datetime fields in case you'd need that.
If you need to grab a set of specific rows then you can use the IN clause:
SELECT * FROM `table` WHERE `id` IN (1, 2, 3, 4, 7, 9, 18, ...)
Most modern ORM's should be able to handle this by simply passing them an array. If you still want to construct your queries manually (mindboggling prospect) then you can simply implode an array of ids you want.
|
|
I've got an interview tomorrow for an entry level Java position. Been reading up on basic interview questions, but if anyone has advice I'd love to hear it!
|
On November 25 2015 10:18 WarSame wrote: I've got an interview tomorrow for an entry level Java position. Been reading up on basic interview questions, but if anyone has advice I'd love to hear it! Most questions will probably be the generic history, motivations, interests, etc. Don't bullshit those to make it sound better, but try to make it relate to your interest in your work.
Don't be afraid to say that you Google or go to StackOverflow to find answers.
If they give you programming tests, know how to vocalize your steps. Those kind of questions are less about knowing the solution than they are evaluating your coding ability (most importantly: can you actually code) and your thought process.
|
On November 24 2015 07:22 obesechicken13 wrote:EDIT: SolvedI have an issue where I have to do a batch insert/update in Java. I'm using spring boot and hibernate jpa. Currently it takes 10s to do 1000 records using the simple CrudRepository. Under 1s to do 30 records. So it'll take 10 days to insert 100M records and then switch the databases. I can't wait that long. It might be possible to just update the changed records but we're using Oracle so I'm not sure if possible. ... @Service public class AttributeServiceImpl implements AttributeService { @Value("${hibernate.jdbc.batch_size}") private int BATCH_SIZE;
@Autowired private EntityManagerFactory emf;
...
@Override public void insertBatchCollectorAttributes(List<Attribute> attributes) { EntityManager entityManager = emf.createEntityManager(); EntityTransaction tx = entityManager.getTransaction(); tx.begin(); for (int i = 0; i < attributes.size(); i++) { Attribute attribute = attributes.get(i); entityManager.persist(attribute); if (i % BATCH_SIZE == 0) { entityManager.flush(); entityManager.clear(); } attributes.add(attribute); } entityManager.flush(); entityManager.clear(); entityManager.close(); } } Every time it gets to entityManager.flush() the method hangs. Looking into the source code it hangs on this: // Invoke method on current EntityManager. try { return method.invoke(this.target, args); How do I get the code to stop hanging. Thank you TL rockstars and thank you! 
Not sure about Java, but I'm almost certain you shouldn't use flushes inside loops. Persist your entities inside the loop and only flush once after the loop is done. This way you only execute a single query instead of a query for each loop pass.
|
My interview went pretty well. In the last few years I've become more and more comfortable talking with people, so I didn't feel nervous heading into this interview. I also felt I had good preparation for the software side of things from my education combined with the work I've been doing on my own for the last few months.
The interview had a "social" and programming aspect. The social part was along the lines of "Why did you get into programming?/Tell me a little about yourself." which went swimmingly. They then gave me a 45 question MC sheet. It was surprising how many knowledge questions were on there, and how little thinking questions. For example, one question asked - in what version did Java release Enum. I know Enums are a fairly basic class so I assumed they were released in the initial version, 1.0. They were released in 5.0 - released in 2004. I was 11 when it was released. I don't know how I was supposed to know that. It's worrying because I felt I could have done well with normal coding questions, but my "in-depth" knowledge of Java is not very good. I've never had to worry if a top level class can be declared private or not in my work, so I just flat out didn't know.
Overall I went through and got about 60% of the questions off the bat, another 30% by narrowing down options and thinking through them, and the last 10% I just tried to knock off options and guess. I don't feel the test adequately tested me for my programming knowledge, so that's worrisome. Hopefully I did well enough...
|
Honestly, any company that thinks it is important to know in what version of Java they released Enums, is not a company I would want to work for. There are two possible reasons for needing to know that: 1. You are working on ridiculously bad enterprise software that uses different legacy versions of Java, and you need to know the distinctions between the versions well to be able to tinker with all its parts. This is software I wouldn't want to touch with a ten-foot pole. 2. They are douches.
So either way, I would probably just walk out of the interview if I got that question. The fact that you DIDN´T walk out means you either want the job more than I would, or you´re just a better person than me :D
|
|
|
|