Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.luv2code.hibernate.demo;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import com.luv2code.hibernate.demo.entity.Student;

public class CreateStudentDemo {
public static void main(String[] args) {

for (int i = 0; i < 10; i++) {
System.out.print(sum(i) + " , ");
}

SessionFactory factory = new Configuration()
.configure("hibernate.cfg.xml")
.addAnnotatedClass(Student.class)
.buildSessionFactory();
// create session
Session session = factory.getCurrentSession();

try {
// use the session object to save java object .
// create a student object
// start a transaction
// save the student object
// commit transaction
} catch (Exception e) {
// TODO: handle exception
}finally {
factory.close();
}
}

public static int sum(int n) {
int a = 0,b =1,c=0;
if(n == 0)
return a;
else if(n == 1)
return b;
for (int i = 2; i <= n; i++) {
c = a+b;
a = b;
b = c;
}
return c;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package com.luv2code.hibernate.demo.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="student")
public class Student {

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="id")
private int id;

@Column(name="first_name")
private String firstName;

@Column(name="last_name")
private String lastName;

@Column(name="email")
private String email;

public Student() {

}

public Student(String firstName, String lastName, String email) {
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
}

public int getId() {
return id;
}

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

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

@Override
public String toString() {
return "Student [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", email=" + email + "]";
}

}




32 changes: 32 additions & 0 deletions hibernate-tutorial/src/com/luv2code/jdbc/TestJdbc.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.luv2code.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;

public class TestJdbc {

public static void main(String[] args) {

String jdbcUrl = "jdbc:mysql://localhost:3307/hb_student_tracker?useSSL=false&serverTimezone=UTC";
String user = "root";
String pass = "";

try {
System.out.println("Connecting to database: " + jdbcUrl);

Connection myConn =
DriverManager.getConnection(jdbcUrl, user, pass);

System.out.println("Connection successful!!!");

}
catch (Exception exc) {
exc.printStackTrace();
}

}

}



29 changes: 29 additions & 0 deletions hibernate-tutorial/src/hibernate.cfg.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

<session-factory>

<!-- JDBC Database connection settings -->
<property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3307/hb_student_tracker?useSSL=false&amp;serverTimezone=UTC</property>
<property name="connection.username">root</property>
<property name="connection.password"></property>

<!-- JDBC connection pool settings ... using built-in test pool -->
<property name="connection.pool_size">1</property>

<!-- Select our SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>

<!-- Echo the SQL to stdout -->
<property name="show_sql">true</property>

<!-- Set the current session context -->
<property name="current_session_context_class">thread</property>

</session-factory>

</hibernate-configuration>
6 changes: 6 additions & 0 deletions spring-mvc-demo/src/com/luv2code/springdemo/mvc/Student.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
package com.luv2code.springdemo.mvc;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

public class Student {
private String firstName;
private String lastName;
private String country;
@NotNull
private String favoriteLanguage;
@Size(min = 1)
private String favoriteOs[];
public Student(String firstName, String lastName) {
this.firstName = firstName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

Expand Down Expand Up @@ -37,7 +40,9 @@ public String showFourm(Model model) {
}

@RequestMapping("/processForm")
public String processForm(@ModelAttribute("student") Student theStudent) {
public String processForm(@Valid @ModelAttribute("student") Student theStudent ,BindingResult result) {
if(result.hasErrors())
return "student-form";
return "student-confirmation";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
@Target({ElementType.METHOD,ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface CourseCode {
// define default corse code
// define default course code
public String value() default "LUV";
// define default error message
public String message() default "must start with LUV";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public void initialize(CourseCode constraintAnnotation) {
@Override
public boolean isValid(String theCode, ConstraintValidatorContext theConstraintValidatorContext) {
if(theCode == null) {

return true;
}else {
return theCode.toUpperCase().startsWith(coursePrefixes.toUpperCase());
Expand Down