Created
July 7, 2022 18:14
-
-
Save FranckPachot/fcd11b5a63b7512cfe3404ed61a3fa53 to your computer and use it in GitHub Desktop.
Testing PostgreSQL text in Hibernate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
cd /var/tmp | |
wget -c https://netix.dl.sourceforge.net/project/hibernate/hibernate-orm/5.6.0.Final/hibernate-release-5.6.0.Final.zip | |
unzip -qo hibernate-release-5.6.0.Final.zip | |
for i in hibernate-release-5.6.0.Final/lib/required/*.jar | |
do | |
CLASSPATH="${CLASSPATH}:$PWD/$i" | |
done | |
wget -c https://jdbc.postgresql.org/download/postgresql-42.3.0.jar | |
export CLASSPATH=.:$ORACLE_HOME/jdbc/lib/ojdbc8.jar:$PWD/postgresql-42.3.0.jar:$CLASSPATH | |
cd - | |
javac PostgresText.java && java PostgresText && | |
psql -e -c "\\d unlimited_text" -c "select * from unlimited_text" postgresql://yb1.pachot.net:5433/yugabyte | |
*/ | |
import java.io.*; | |
import java.sql.*; | |
import java.util.*; | |
import org.hibernate.*; | |
import org.hibernate.annotations.Type; | |
import org.hibernate.cfg.*; | |
import javax.persistence.*; | |
public class PostgresText {@Entity(name = "PostgresText") | |
@Table(name = "unlimited_text") | |
public class UnlimitedText { | |
@Id | |
@GeneratedValue(strategy=GenerationType.SEQUENCE) | |
private Long id; | |
//@Column(length=10485760) | |
//@Column(columnDefinition="text") | |
//@Column(columnDefinition="oid") | |
//@Lob | |
//@Type(type="org.hibernate.type.TextType") | |
//@Type(type="org.hibernate.type.StringType") | |
private String name; | |
public Long getId() { | |
return id; | |
} | |
public void setId(Long id) { | |
this.id = id; | |
} | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
@Override | |
public boolean equals(Object o) { | |
if (this == o) return true; | |
if (!(o instanceof UnlimitedText)) return false; | |
UnlimitedText unlimitedText = (UnlimitedText) o; | |
return Objects.equals(getName(), unlimitedText.getName()); | |
} | |
@Override | |
public int hashCode() { | |
return Objects.hash(getName()); | |
} | |
} | |
public void run(){ | |
SessionFactory sf=new Configuration() | |
.addAnnotatedClass(UnlimitedText.class) | |
.setProperty("hibernate.connection.url","jdbc:postgresql://yb1.pachot.net:5433/yugabyte") | |
.setProperty("hibernate.connection.driver_class","org.postgresql.Driver") | |
.setProperty("hibernate.dialect","org.hibernate.dialect.PostgreSQLDialect") | |
.setProperty("hibernate.connection.username","yugabyte") | |
.setProperty("hibernate.connection.password","yugabyte") | |
.setProperty("hibernate.format_sql","true") | |
.setProperty("hibernate.show_sql","true") | |
.setProperty("hibernate.hbm2ddl.auto","create") | |
.buildSessionFactory(); | |
UnlimitedText c1 = new UnlimitedText(); | |
c1.setName("Hello World"); | |
Session s=sf.openSession(); | |
Transaction t=s.beginTransaction(); | |
s.persist(c1); | |
s.flush(); | |
t.commit(); | |
t=s.beginTransaction(); | |
UnlimitedText c2 =s.get(UnlimitedText.class,1L); | |
System.out.print("========================"+c1.getName()+"=======\n"); | |
t.commit(); | |
} | |
public static void main(String[] args) throws SQLException { | |
PostgresText main = new PostgresText(); | |
main.run(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment